- Home /
Getting an object to re-spawn/ transform player back to the beginning
I am creating a pretty basic platform game for a project in college and pretty much new to coding. I am just wandering what would be the correct code to apply if I wanted to make a certain object cause the player to transform back to the starting point (Like instant death but the player can carry on without game over)?
Answer by FlaSh-G · Apr 02, 2020 at 09:23 PM
You can set the any object's transform.position at any point. For example, you could create a new GameObject that serves as a respawn point, define a field where you drag that object into and set the player's position to that object's position at the right time.
Note: Code quality reduced for the sake of simplicity.
public Transform respawnPoint;
public Transform player;
public void RespawnPlayer()
{
player.position = respawn.position;
}
How and when you call RespawnPlayer
depends completely on how your game works, and very likely, you'll want to grab a reference to the player in another way than dragging it into the inspector, as you'd do with this code.
Your answer
Follow this Question
Related Questions
Error says object reference not set when it really is? 1 Answer
2D Platformer - How do I have objects that kill the player upon touch/cause respawn? 2 Answers
2D 360 degress platformer example needed 0 Answers
Respawn question 2 Answers
Player slides off from moving platform when player isn't moving Bolt/visual scripting 0 Answers