- Home /
First Person Player Teleportation
I'm sure this dead horse has probably been beaten many times over, but I can't seem to find an answer to my particular problem...
I am attempting to set up up a simple trigger to teleport the first person character to another location with the following script:
void OnTriggerEnter(Collider trig)
{
if(trig.gameObject.tag == "WarpTrig")
{
transform.position = destination.transform.position;
}
}
This script is currently attached to the FPS Controller. I can see that the player is teleported for what seems like one frame, but then the player is right back to standing in the trigger. Your help is much appreciated!
Answer by boris1709 · Nov 07, 2019 at 04:45 AM
Sorry for bumping this, but I found a fix to the problem caused by the FirstPersonController.cs script.
If you teleport the character, simply turn off the FPS controller script, and then set a boolean to true that will turn the script back on the next Update() call.
An example is below
void Start()
{
turnOnMovementNextTick = false;
}
void Update()
{
if (turnOnMovementNextTick)
{
movementScript.enabled = true;
turnOnMovementNextTick = false;
}
if (Input.GetKeyDown(KeyCode.L))
{
movementScript.enabled = false;
transform.position = new Vector3(15.32f, 0.97f, 9.54f);
turnOnMovementNextTick = true;
}
}
Answer by herzog_deair · Apr 21, 2019 at 09:14 AM
First Person Controller script keeps the player back at old position. If I disable that script, transform.position works very well, but then I can't move the player :D
Answer by nik_bluebird · Apr 19, 2019 at 11:33 AM
Hi,
OnTriggerEnter() event only called when you attached Collider component on both game objects and one of them should have attached Rigidbody component. Also make sure, 'Is Trigger' property should be checked, in any one of Collider.
For more details:
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html
Answer by jemmysponz7 · Oct 13, 2019 at 10:15 AM
It's been a bit since you posted this, but I had the same question! After some digging, I found what worked best for me was briefly enabling autoSyncTransforms during when the teleportation was actually happening. https://docs.unity3d.com/ScriptReference/Physics-autoSyncTransforms.html
THANK YOU! It worked for me. Actually, not autoSyncTransformsms, but Physics.SyncTransforms() right after the transform.position change. It tried to make me nuts. Thank you again.
Answer by Kieran-Brown · Apr 26, 2020 at 03:39 AM
This is a dead horse, and I am sorry for making it walk around like a zombie 1 year later.... But as herzog_deair said, the way the FirstPersonController prefab from standard assets is coded has it's own set of rules, I am fairly new to Unity and was struggling a lot with this for months trying to make a teleport work.
The very simple fix I found is to just use the RigidBodyFirstPersonController prefab (or something like that), any of the other standard assets prefabs should work fine with transform.position, they may need a little polishing though to meet your desired needs, but they work.
Your answer
Follow this Question
Related Questions
FPS teleport flicker one frame- but works fine with main camera. 2 Answers
First Unity tryOut - First Person Controller makes all objects just Float away... 3 Answers
Move Character Controller along Box 0 Answers
Sphere Controller Help? 1 Answer
Project at correct angle using First Person controller 1 Answer