Hey there. Im really new to unity and am working on one of my first games. The game is a parkour type game, and Im wondering How to create a killbox (falling off an obstacle into the void, but instead of just falling infinitely you just die and respawn). Im also wondering How you would create a respawn point.
Note: Im not using lives health, or anything. Im basically looking for: Touch this object get teleported to spot
Look into OnTriggerEnter2D. You can create an Empty gameobject called DeathTrigger or RespawnTrigger or whatever and have a BoxCollider2D on it. Set that collider to a trigger. Also, make a tag for those DeathTrigger objects to something, whatever you name that tag, make sure every DeathTrigger has that tag (look at prefabs probably).
On your player, make sure they have a collider2D as well, but NOT set to trigger. If it is a trigger it won’t react to physics. On your player have a small script that only deals with “death” and respawning. Use an OnTriggerEnter2D function like this:
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "DeathTrigger")
{
transform.position = respawnPos;
}
}
Then, have a public respawnPos variable of the type Transform. Create an empty gameobject called RespawnPoint and place it where you want in your scene. Then, in the inspector on that script, drag that RespawnPoint gameobject to the public field for respawnPos.