- Home /
Reset posiotion
I have a player that I want to be placed to his last position if he "die". I do not know how I can do this. So, I need a code that repositionates my player to the exact position and rotation he started when I open the scene. I do NOT want to use: Application.LoadLevel("My_Level"); I appereciate answers.
Answer by _joe_ · Mar 28, 2015 at 07:24 PM
First start by saving your initial position when the game starts, then when you die, reset the position to that saved position:
//Class variable
public Vector3 myPlayerInitialPostion;
void Start ()
{
//Save the position when the game starts
myPlayerInitialPostion = this.transform.position;
}
//This is the function that you need to call when the character dies
public void ResetPosition(){
this.transform.position = myPlayerInitialPostion;
}
Thank you very much, it did work! I also coded that the rotation resets, and that worked just fine. One thing: The players "gravity" (I don't know which word to use) is still there. When my player dies, he bounces away when he repositionates. How can I make the players gravity be like his gravity is when the game starts. Again thanks for answering.
Sure, you can reset everything by setting the velocity to 0:
this.gameObject.rigidbody.velocity = Vector3.zero;
Thank you for accepting the answer and voting it up :)
I do not quite understand.. When I put this code where the character dies, it says that it does not contain a defenition for velocity. And by the way, im using rigidbody2D. ($$anonymous$$aybe that has somthing to do with the error, I don't know.)
Of course, use rigidbody2D ins$$anonymous$$d:
this.gameObject.rigidbody2D.velocity = Vector2.zero;
Please select the question as Answered by selecting the correct one.
Your answer
![](https://koobas.hobune.stream/wayback/20220613191501im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
make player move in direction it's facing 2 Answers
Game Object won't match rotation of new positions transform 1 Answer
Call Other Object to Self 2 Answers
Camera rotation around player while following. 6 Answers
Saving System? 1 Answer