0
\$\begingroup\$

Currently, I have a 2D platformer game, and I want to implement a function that can make the player die and go back to the start or a checkpoint when he interacts with a weapon or trap.

What I have in my scene is a sprite with a Rigidbody on it and a trap sprite with a BoxCollider2D on it which is set to trigger.

How can I make my player go back to the start when he hits the trap?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Are you keeping track of the checkpoints? Do you need to reset the scene (dead monster respawn/ you loose earned items/ etc)? \$\endgroup\$ Commented Feb 2, 2021 at 11:25
  • \$\begingroup\$ I want to reset to the closet checkpoints t however I do that. I don't need to worry about items \$\endgroup\$ Commented Feb 2, 2021 at 11:28

2 Answers 2

1
\$\begingroup\$

Since you do not need to reset the scene, all you need to do is

  • Save the transform of the last checkpoint when you reach it.
  • Destroy the current player.
  • Instantiate the current player around the saved transform.
  • Move the camera back to the player if it not done already.
\$\endgroup\$
4
  • \$\begingroup\$ the camera follows the paltry, so how do I make the trap do damage \$\endgroup\$ Commented Feb 2, 2021 at 11:34
  • \$\begingroup\$ do I put a script on the trap that kills the player or do I need something on the player \$\endgroup\$ Commented Feb 2, 2021 at 11:37
  • \$\begingroup\$ may I ask how I do any of that as I'm very new to unity \$\endgroup\$ Commented Feb 2, 2021 at 11:48
  • 2
    \$\begingroup\$ Start with the Unity 2D platform tutorial to get some basics and ask a clear question where you struggle when you try to implement it on your own (with the error and what you did) \$\endgroup\$ Commented Feb 2, 2021 at 11:53
0
\$\begingroup\$

Add this script into your enemy. It will reset the scene which in theory will bring the player back to start. Remember to call the UnityEngine and UnityEngine.SceneManagement API services at the start. Also, make sure to have the player tag on the player, so the scene won’t restart if any GameObject collides with the enemy.

  // Initiates when something collides with enemy
        void OnTriggerEnter2D(Collider2D collider) {
    
        if (collider.gameObject.CompareTag(“Player”))
           {
              SceneManager.LoadScene(SceneManager.getActiveScene().buildIndex);
           }
        }
\$\endgroup\$
9
  • \$\begingroup\$ Though the question asks to go to a checkpoint, which could be part from the scene that is not the same as when you load the scene the first time. \$\endgroup\$ Commented Feb 2, 2021 at 14:21
  • \$\begingroup\$ One of your { is at the wrong place, not enough to make it possible to edit it. And yes, the tutorial works because it has a GameManager instance that saves the transform of the checkpoint and loads that one on scene reload. \$\endgroup\$ Commented Feb 2, 2021 at 14:32
  • \$\begingroup\$ ill have 1 scene \$\endgroup\$ Commented Feb 2, 2021 at 16:31
  • \$\begingroup\$ @elliot727 What do you mean exactly? \$\endgroup\$ Commented Feb 2, 2021 at 16:42
  • \$\begingroup\$ I load everything g together \$\endgroup\$ Commented Feb 2, 2021 at 17:10

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.