📢 preface

  • Christmas is coming soon, let’s sort out some small games for learning and entertainment
  • People always say that you can’t study and play games at the same time, so let’s learn how to make games and play games at the same time
  • So this article brings you oneHorizontal version of 2D shooting games, the game production super simple, play a learning will, let’s have a look!

🎬 horizontal version of 2D shooting games

🎥 Game screen display

This small game uses only two UI interfaces, one is the menu interface, the other is the battle interface

The menu interface has three modes, namely Normal, Difficult and Hell

The combat interface is simple: spawn monsters from both sides and shoot them

UI setup is very simple, only a background Image using Image, add a Text input box, and then put the position!

The menu interface is as followsCombat screen demo

🔔 game code analysis

This horizontal version of 2D shooting small game, the core part only need two scripts can be completed, a study will, together with a look at the code!

PlayerMove, a script mounted to the player, controls the player’s left and right movement and jumping

There are also two methods of firing, which are through and through! Blood drops when you touch a monster

The game ends when the player’s health drops below 0, very simple and clear logic!

The code is as follows, one does not have many lines, the code content is Unity’s most basic knowledge point, I believe that people who understand a little can also understand

The PlayerMove code is as follows

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerMove : MonoBehaviour {

    /// <summary>
    ///Life value
    /// </summary>
    public int HP;
    public Slider hpUI;

    /// <summary>
    ///Animation form
    /// </summary>
    public Animator _animator;

    /// <summary>
    ///kill
    /// </summary>
    public static int jisha;
    public Text jishaUI;
    public Text jishaUIEnd;

    /// <summary>
    ///Whether jumping
    /// </summary>
    public bool isJump;

    /// <summary>
    ///End of Game UI
    /// </summary>
    public GameObject ui;
    // Use this for initialization
    void Start ()
    {
        Time.timeScale = 1;
        jisha = 0;
        hpUI.maxValue = HP;
        hpUI.value = HP;
    }
	
	// Update is called once per frame
	void Update ()
    {
        if (Input.GetKeyDown (KeyCode.Escape))
        {
            UnityEngine.SceneManagement.SceneManager.LoadScene("Menu");
        }
        if (HP <= 0)
        {
            HP = 0;
        }
        jishaUI.text = "Kill:" + jisha;
        jishaUIEnd.text = "Kill:" + jisha;
        if (HP == 0)
        {
            ui.SetActive(true);
            Time.timeScale = 0;
        }
        hpUI.transform.position = Camera.main.WorldToScreenPoint(transform.position + new Vector3(0.3.0));
        hpUI.value = HP;
        if (Input.GetKey (KeyCode.D))
        {
            transform.localEulerAngles = new Vector3(0.0.0);
            transform.Translate(Vector3.right * Time.deltaTime * 7);
            _animator.SetBool("run".true);
        }
        else if (Input.GetKey(KeyCode.A))
        {
            transform.localEulerAngles = new Vector3(0.180.0);
            transform.Translate(Vector3.right * Time.deltaTime * 7);
            _animator.SetBool("run".true);
        }
        else
        {
            _animator.SetBool("run".false);
        }
        if(Input.GetKeyDown (KeyCode.Space) && ! isJump) { GetComponent<Rigidbody2D>().velocity =new Vector2(0.30);
        }
        if (isJump)
        {
            _animator.SetBool("Jump".true);
        }
        else
        {
            _animator.SetBool("Jump".false);
        }
        if (Input.GetKeyDown (KeyCode.J))
        {
            GameObject n = Instantiate(Resources.Load("a1"), transform.GetChild(0).position, transform.GetChild(0).rotation) as GameObject;
            n.name = "a1";
            Destroy(n, 2);
        }
        if (Input.GetKeyDown(KeyCode.K))
        {
            GameObject n = Instantiate(Resources.Load("a2"), transform.GetChild(0).position, transform.GetChild(0).rotation) as GameObject;
            n.name = "a2";
            Destroy(n, 2); }}private void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.name == "0")
        {
            isJump = false; }}private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.name == "0")
        {
            isJump = true; }}public void ReturnMenu()
    {
        UnityEngine.SceneManagement.SceneManager.LoadScene("Menu"); }}Copy the code

There is also a Monster script that displays the Monster’s health, speed, damage, etc

There are players hit the bullet will drop blood, a very simple way to achieve!

The complete code is as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Monster : MonoBehaviour {
    /// <summary>
    ///life
    /// </summary>
    public int HP;

    /// <summary>
    ///speed
    /// </summary>
    public float speed;

    /// <summary>
    ///damage
    /// </summary>
    public int att;


    public Slider hpUI;
	// Use this for initialization
	void Start ()
    {
        speed = Random.RandomRange(speed - 2, speed + 2);
        Destroy(gameObject, 10);

        GameObject n = Instantiate(Resources.Load("HP")) as GameObject;
        n.transform.SetParent(GameObject.Find("HPShow").transform);
        n.transform.localScale = Vector3.one;
        hpUI = n.GetComponent<Slider>();
        hpUI.maxValue = HP;
        hpUI.value = HP;
	}
	
	// Update is called once per frame
	void Update ()
    {
        if (HP <= 0)
        {
            HP = 0;
            PlayerMove.jisha++;
            Destroy(gameObject);
        }
        hpUI.transform.position = Camera.main.WorldToScreenPoint(transform.position + new Vector3 (0.6.0));
        hpUI.value = HP;
        transform.Translate(Vector3.right * Time.deltaTime * speed);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        // Hit player health
        if (collision.name == "Player")
        {
            collision.GetComponent<PlayerMove>().HP -= Random.RandomRange(att - 2, att + 2);
            Destroy(gameObject);
        }
        // Red bullets have low damage and can Pierce multiple targets in a row
        if (collision.name == "a1")
        {
            HP -= Random.RandomRange(8.15);
        }
        // Red bullets do high damage and can only hit one target
        if (collision.name == "a2")
        {
            HP -= Random.RandomRange(20.40); Destroy(collision.gameObject); }}private void OnDestroy(){ Destroy(hpUI.gameObject); }}Copy the code

🎄 Game packaging

This small game only need the above two scripts are almost complete, and then can be packaged out to play on the computer!

It’s easy to package the game, just add the following scenes under Build and Build directly.

The folder looks like this, and then click on the.exe file and you can play it directly on your computer!


🎁 game source download

In this horizontal version of the 2D shooting game source project download link here!

Horizontal version of the 2D shooting game source project download link

Small partners with points can download experience, points not enough direct attention to the author’s home page public account reply: game resources

You can get dozens of game source code and material, see if you want that one!


💬 summary

This article shares a super simple side-scrolling 2D shooter mini game, perfect for beginners to learn

What you see is what you get, and you can quickly experience what it’s like to play while you’re playing!

There will be many more games to share, including very simple games like this one

Will also share some of the large game source code and so on, to provide you with a reference to study!

That’s all for this article! Click on a triple and I’ll see you next time