- Home /
How to teleport gameobject (instantly change transform.position)
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.
Answer by Tripleganger · Mar 13, 2020 at 05:41 PM
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.
Yes, because i'm printing a Debug.Log when that happens
The script is attached to a GameObject called Player, which has 6 components, Transform, Cube ($$anonymous$$esh Filter), $$anonymous$$esh Rendered, Box Collider, PlayerController, CharacterController
Yes the script is attached (PlayerController.cs)
It is not, it's in the Update's scope.
Hello. Post then all your Update code where you do the Debug.Log. When do you store the initialPosition vector? Also post a screenshot of the Unity Editor Inspector of the Player GameObject.
The initalPosition vector is stored on general scope and initialized on Start method.
void Update()
{
bool touchedJoystick = joystick.Horizontal != 0 || joystick.Vertical != 0;
if (!started$$anonymous$$oving && touchedJoystick) {
game$$anonymous$$anager.StartedLevel();
started$$anonymous$$oving = true;
}
if(transform.position.y <= 2f){
Debug.Log("Reset");
gameObject.transform.position = new Vector3(initialPosition.x, initialPosition.y, initialPosition.z);
}
float horizontal = joystick.Horizontal;
float vertical = joystick.Vertical;
Vector3 input = new Vector3(horizontal, 0f, vertical);
Vector3 inputV2 = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y, 0) * input;
controller.Simple$$anonymous$$ove(inputV2 * 9);
if(inputV2 != Vector3.zero)
transform.rotation = Quaternion.LookRotation(inputV2, Vector3.up);
Vector3 gridPos = ConvertWordCor2TerrCor(transform.position);
int x = (int)gridPos.x;
int y = (int)gridPos.z;
coverCircleArea(x, y, 15);
}
I have tried to reproduce the whole thing and... You are right, it does not work. So I made it in my own terms, which are only slightly different from yours.
Declare`private Vector3 v3_Original;` as a member (variable).
In Update (Or even better, LateUpdate) write` if (gameObject.transform.position.y <= 2f) { gameObject.transform.position = v3_Original; }`. Attach this to your player and it will work, as I have just tested. Here's the full script:
private Vector3 v3_Original; // Start is called before the first frame update void Start() { v3_Original = gameObject.transform.position; Debug.Log(v3_Original); } // Update is called once per frame void Update() { if (gameObject.transform.position.y <= 2f) { Debug.Log(v3_Original); gameObject.transform.position = v3_Original; } }
oh it works if i put it on LateUpdate lol, if i put the same piece of code in Update it does not work. Thanks! :)
A lot of times this is due to the physics engine not being in sync with the changes. You can calls Physics.SyncTransgorms after the position change to make it work in Update
https://docs.unity3d.com/ScriptReference/Physics.SyncTransforms.html
Answer by aoun111 · Mar 14, 2020 at 06:45 AM
If you use the FPS Controller, disable that script and the Character controller before teleporting, and enable them again after teleporting
Answer by Supphakon · Apr 13, 2020 at 01:10 PM
GameObject.tranfrom.position = new Vector 3 (psc x ,pos y , pos y)