I’m trying to do a “quick restart” of a level kind of thing. So in order to do this i’m just saving the initial position of the Player on the Start method as such:
initialPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
Then on Update, everytime the player goes to an area it shouldn’t, then it should go back to the initial position.
if (transform.position.y <= 2f){
gameObject.transform.position = new Vector3(initialPosition.x, initialPosition.y, initialPosition.z);
}
This does absolutely nothing… and all other answers say to do exactly this…
Any help? Thanks in advance.
Hey @Serge114! First, instead of gameObject.transform.position = new Vector3(initialPosition.x, initialPosition.y, initialPosition.z);
you can write gameObject.transform.position = initialPosition;
.
Then, regarding the main issue:
- Are you sure your object’s position is ever going to be <= 2f?
- Remember that
transform.position
refers to the object the script is attached to; if this script is not attached to your player, it will not work.
- Have you actually assigned the script to your player?
- Is
if (transform.position.y <= 2f){ gameObject.transform.position = new Vector3(initialPosition.x, initialPosition.y, initialPosition.z); }
nested inside another if statement that is never valid?
Hope this helps.
If you use the FPS Controller, disable that script and the Character controller before teleporting, and enable them again after teleporting
GameObject.tranfrom.position = new Vector 3 (psc x ,pos y , pos y)