Need player to die when they touch the floor. Never really coded before so I have no idea how to get his effect.
So, I'm making a simple platforming game with some friends but I can't figure out how to get the player character to destroy when they touch the floor of my level. I need to get the character to be destroyed and re-spawn at a checkpoint I have set in the level. Can anyone help with this?
There are a lot of things involved, i would recommend you to look for a plataformer tutorial on youtube, but the basic code is:
//Check if your character touched some trigger.
private void OnTriggerEnter2D(Collider2D collision)
{
//Check if the trigger is the floor
if (collision.gameObject.tag == "Floor")
{
//set the position of the player same as the checkpoint.
transform.position = checkpoint.position;
}
}
I'm assu$$anonymous$$g your game is 2D, note that for this code to work your Player game object need a Rigidbody2D and your floor must be tagged Floor and have a Collider2D with the trigger checkbox checked. Also your player script will need a reference to the checkpoint transform so that you can call transform.position = checkpoint.position;
Check out this tutorial:
Your answer
![](https://koobas.hobune.stream/wayback/20220612105110im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Respawn not working past 2nd respawn 1 Answer
Why is my player not being found after being respawned 2 Answers
Problem when respawning a rigidbody in the desired position using rigidboy.position. 0 Answers
When player respawn, enemy cant hit player 0 Answers
destroying objects when answering a question correctly 1 Answer