- Home /
Falling off respawn script
Greetings, I am currently creating a game where you have to jump from one cube to another and not fall off. I was wondering if there was any way that I could make it so the player respawns at the original spawn point after falling a little distance. Does anyone have scripts for my request? I am having a very hard time with this as I am quite new to Unity. May you explain in detail exactly what I do to have the player respawn on the original spawn point after falling off? I already created a invisible large rectangle under the map so the player falls off and lands on it. Now what do I do after this? How do I make it so the player teleport to the spawn point after hitting the invisible platform? I have set up something so that if the player falls off, it restarts the scene, but I don't like it. I want it so that my other scripts (Such as a timer or lives) do not restart when the player falls off.
I have given answer to your question in http://answers.unity3d.com/questions/339115/falling-off-respawn.html
Create player a prefab(Drag the Player(with scripts and all) from the hirarchy to the project panel) Assign the player prefab to the player in my script;
Answer by speedything · Oct 30, 2012 at 10:50 PM
You need to keep track of the last block your person is on. Then, when he hits the invisible plane, you change his position to where he was at that time.
Do something like...
(on player GameObject)
public Vector3 lastGoodPosition;
void OnCollisionEnter(Collision collision)
{
if (collision.tag == "Block")
{
lastGoodPosition = transform.position;
}
else if (collision.tag = "PlaneOfDeath")
{
transform.position = lastGoodPosition;
}
}
You may need to tidy this up a little bit. For example, if the player hits a block as they're falling they'll end up in a cycle of death. However, this should get you started on the right path.
I can't edit this code, but it'd actually be (collision.collider.tag == ... ) and the else if condition is missing a '='.
Your answer
Follow this Question
Related Questions
Falling off respawn 4 Answers
Finding the player that walked into a ray? 1 Answer
Respawn after falling off script? 2 Answers
Help - Player Respawn Help 2 Answers
2D Movement Problems 2 Answers