[unity] Share a complete production tutorial for a 2D mini game - Graffiti Jump demo

Preface

1. Mapping preparation

  • The tools used are ps, cut a board, cut a protagonist, and find a background wall so that the materials are ready~

Logic of the Springboard

  • After putting the springboard into the scene, a collider component is given because collisions are involved

  • The next detail is that if the task touches the bottom of the board, it will not count as stepping on the springboard to get up speed! This requirement can be achieved in two ways (one is to judge a collision callback when the character falls; the other is to use the Platform Effector 2D below) Note that the use by effector in collider needs to be hooked

  • Finally, give the board a tag to judge the collision, so you can use the board as a prefabricated part and drag it away.

The Logic of Characters

  • First people are gravitated, so add the Rigibody2D component and give it a scale of gravity
  • Collisions need to be calculated, so give a collider! Remember to tick Is Trigger, no, we don't use the OnTriggerEnter2D method of collision callback, because the character stops on the board and doesn't enter!
  • Let's give the role a script to control
  • The following script has the ability to view left and right crossings (when the left crosses the border, the characters will come out from the right, it looks like the left and right maps are not out of bounds, they are connected)
  • Characters get an upward speed when they touch the board
  • Characters are controlled by the arrow keys (Details: Character orientation is also affected by left-to-right orientation)
public class Player : MonoBehaviour
{
    public Rigidbody2D rb;
    public float sensitive = 10f;


    private void Update()
    {
        PlayerController();
        //View out of bounds
        checkOverEdge();
    }

    private void checkOverEdge()
    {
        if (transform.position.x < -6f)
        {
            transform.position = new Vector2(6f, transform.position.y);
        }
        if (transform.position.x > 6f)
        {
            transform.position = new Vector2(-6f, transform.position.y);
        }
    }

    private void OnTriggerEnter2D(Collider2D col)
    {
        //Collisions are calculated when falling. Otherwise the character will fly up whenever he touches something
        if (rb.velocity.y <= 0)
        {
            if (col.CompareTag("board"))
            {
                rb.velocity = new Vector2(0f, 10f);
            }
        }

        if (col.CompareTag("win"))
        {
            SceneManager.LoadScene("Win");
        }
    }

    private void PlayerController()
    {
        float horizontalAxis = 0;
        horizontalAxis = Input.GetAxis("Horizontal");
        if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
        {
            transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
        }

        if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
        {
            transform.rotation = Quaternion.Euler(new Vector3(0, 180, 0));
        }

        rb.velocity = new Vector2(horizontalAxis * sensitive, rb.velocity.y);
    }


Camera Logic

  • Camera is the same thing as a game of jumping circles with a small ball. If role y is larger than camera y, the camera follows up
  • If a character falls down (a certain size from the y-axis of the camera, counts as a failure, reloads the game scene), the game returns to its starting point~
public class PlayerFollow : MonoBehaviour
{
    public Transform PlayerTransform;
    private Vector3 currentVelocity;
    public Rigidbody2D playerRigidbody2D;

    private void LateUpdate()
    {
        if (PlayerTransform.position.y >= transform.position.y)
        {
            Vector3 tmpv = new Vector3(transform.position.x, PlayerTransform.position.y, transform.position.z);
            transform.position =
                Vector3.SmoothDamp(transform.position, tmpv, ref currentVelocity, .3f * Time.deltaTime);
        }

        //Fall to the bottom of the valley, back to the origin
        if (PlayerTransform.position.y < transform.position.y - 3f)
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        }
    }
}

How to automatically generate a springboard

  • In the scene, there is a special empty object that is used to automatically generate boards. It's an empty object, just place it at the origin, so it's easy to generate a map.

  • The logic to generate a map here is to generate all the board positions for this level at the start of the game and at Start

  • In the following script, numberOfPlatforms-1 number of boards are randomly generated according to a certain height difference and a certain level difference, and a win point is generated at the last time (in the game, a red flag, touching wins!)

  • The horizontal distance of the board in the game cannot be too wide to skip, so write a while loop here and regenerate it if it is too wide.

public class LevelGenerator : MonoBehaviour
{
    public GameObject platformPrefab;
    public int numberOfPlatforms;
    public GameObject winPointPrefab;
    public float levelWidth = 3f;
    public float minY = .2f;
    public float maxY = 1.8f;
    private float lastx;

    private void Start()
    {
        Vector3 spawnPosition = new Vector3();
        for (int i = 0; i < numberOfPlatforms; i++)
        {
            spawnPosition.y += Random.Range(minY, maxY);
            float tmpx = Random.Range(-levelWidth, levelWidth);
            while (Math.Abs(tmpx - lastx) > 4f)
            {
                tmpx = Random.Range(-levelWidth, levelWidth);
            }

            lastx = tmpx;
            spawnPosition.x = tmpx;

            if (i == numberOfPlatforms - 1)
            {
                //Generate win point
                Instantiate(winPointPrefab, spawnPosition, Quaternion.identity);
            }
            else
            {
                Instantiate(platformPrefab, spawnPosition, Quaternion.identity);
            }
        }
    }
}

How to Win the Game

  • Make a marker (red flag here), give it a tag, as a prefabrication, which is automatically generated
  • Load a win scene when a character touches it~
  • This fun and decompressed Demo is done! Specifically, you can view the project code~

Keywords: C# Unity

Added by HaVoC on Tue, 21 Dec 2021 22:50:34 +0200