Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to create Checkpoints in a 2D Platformer.

Discussion in '2D' started by unity_13glassj, Jun 8, 2020.

  1. unity_13glassj

    unity_13glassj

    Joined:
    Jun 8, 2020
    Posts:
    4
    Hi
    I'm a relatively new Unity user, and I haven't been able to work out how to create checkpoints for my platformer. I have looked at various online tutorials, but have not been able to create a functioning checkpoint. Ideally, I would have multiple checkpoints per level. Can anyone help me with the scripting (cs) ?
    Thanks
     
  2. EdoC-QWERTY

    EdoC-QWERTY

    Joined:
    Feb 1, 2020
    Posts:
    75
    I am new to Unity too but i think i can give you some suggestions. Probably you already know some of the stuff since you watched tutorials.

    I assume you already know how to manage the data for the player, like the Health of your character or mana or other things like that.

    I suggest you to create Empty Game Objects with Box colliders set with tiriggers (with "is trigger" box checked in inspector.)
    Add a checkpointScript component too.

    You place the chekpoints gameobjects in the exact position you want the player to spawn and then manipulate the colliders to decide the area when a new checkpoint is activated.
    (Maybe create a checkpoint prefab maybe be useful)

    As for the coding part. There should be something like the game manager (and empty object with a script component GameManagerScript) in your game that handles, for example, when the player dies or other stuff.

    (For the coding stuff i can say things you already know but i'll still try my best)

    So.
    In the Game Manager Script.

    Create a public variable Vector2 like _respawnPoint.
    Create a Custom Function that is triggered when the player dies (health = 0) like "On Player Death". In this function the player is teleported to _respawnPoint and all other attributes are reset (like health) and the player will respawn in _respawnPoint.

    public Vector2 _respawnPoint;


    Code (CSharp):
    1. void Update
    2. {
    3. if(health==0) // check when the player dies and call the function
    4. {
    5. OnPlayerDeath();
    6. }
    7. }
    Code (CSharp):
    1.     public void OnPlayerDeath()
    2.     {
    3.  
    4.        
    5.         player.transform.position = _respawnPoint;
    6.         player.GetComponent<PlayerStats>().health = _maxHealth;
    7.         player.GetComponent<PlayerStats>().mana= _maxMana;
    8.  
    9.  
    10.     }
    In the checkpointScript.

    You register when the player reach the checkpoint and update the _respawnPoint variable os it will respawn in the last checkpoint reached.
    Create a reference to the game manager. To assign this just drag GameManager Object from the scene to the field in inspector.

    public GameObject GameManager;

    Code (CSharp):
    1.  
    2. void OnTriggerEnter2D(Collider2D other)
    3.     {
    4.         Vector2 pointupdate; // local variable to store position of the current game object
    5.  
    6.         if (other.tag == "Player") // filter the objects that collide with the checkpoint. You can assign the tag in the inspector
    7.         {
    8.           // Grab the X and Y values of the checkpoint position
    9.           pointupdate == new Vector2( transform.position.x,transform.position.y);
    10.           // Update the _respawnPoint varaible in GameManager
    11.           GameManager.GetComponent<GameManagerScript>()._respawnPoint == pointupdate;
    12.        
    13.         }
    14.  
    15.  

    There might be some errors but this is how i would do it. Now when the player dies it will respawn on the last checkpoint. Obviously this script does not consider animations, and other stuff like UI elements to show that the checkpoint has been reached it's just the basic.
     
    Last edited: Jun 8, 2020
    Codeyarch and unity_13glassj like this.
  3. unity_13glassj

    unity_13glassj

    Joined:
    Jun 8, 2020
    Posts:
    4
    Thanks, that was really helpful