- Home /
Teleporting Character - issue with transform.position in Unity 2018.3
I am having some issues with teleporting my character in Unity 2018.3.7f1. I am using the FPSController, I created a "portal" with a capsule collider that has the "is Trigger" check marked. I also have created an Empty GameObject as a destination for the character to teleport to. I have a script that is below. It is attached to the portal with the capsule collider.
<--->
public Transform warpTarget;
public Transform thePlayer;
void OnTriggerEnter(Collider other){
thePlayer.transform.position = warpTarget.transform.position;
}
I have attached the empty game object to the warpTarget variable in the Inspector window and I attached the FPSController to the thePlayer variable in the Inspector window. It still doesn't work.I teach a HS game design class and my lab was updated to 2018.3 2 weeks ago. Prior to updating, I had 2018.2 and it worked. It only stopped working after updating to 2018.3.
Does anyone have any work arounds or insight on how to get this to work? Any input is appreciated.
Answer by Zarenityx · Mar 21, 2019 at 04:10 AM
Hard to tell from your question what the actual problem is and what isn't working.
If you're using the built-in FPScontroller, you're using the Character Controller, and the Character Controller has its own internal sense of its position and velocity. I'm not an expert on the inner workings of the character controller, but I know from encountering a similar bug that the character controller doesnt take kindly to being teleported around.
The solution I ended up using was pretty simply to turn the Character Controller off before teleporting, then on after. As simple as:
CharacterController cc = thePlayer.GetComponent<CharacterController>();
cc.enabled = false;
thePlayer.transform.position = warpTarget.transform.position;
cc.enabled = true;
When the Character Controller is re-enabled it should grab whatever position its at and start working from there.
Hope this works for you. This is what I had to do to get teleportation working.
So, you were correct on it being the character controller. However, your script didn’t work. After reading your reply about the character controller I did another search. Found the resolution from Unity. Go to Edit > Project Settings > Physics and then check the box “Auto Sync Transforms”. Fixes the issue. Thank you for bringing the character controller to my attention!
Ah. $$anonymous$$akes sense. Sorry about that. When I implemented my teleport it happened in FixedUpdate, so it didn't end up being necessary.
This did it for me, I can't thank u enough. No where is transform sync mention in the documentation.
It worked for me, thanks ! Getting the transform position of a Character Controller is really strange, I couldn't have imagined it. Thanks for the answer to this ! I was begining to think that it was an unity bug
Answer by Ahmed_25 · Mar 20, 2019 at 07:48 AM
There will be a problem created by Vectors
Could you elaborate or provide an alternate solution?
Answer by PerfectSkies · Mar 20, 2019 at 04:10 PM
Try this for TP script:
Vector3 tpLocation = new Vector3(13f, -12f, 0f); //Or warpTarget.transform.position.
Quaternion startRotation = Quaternion.Euler(Vector3.zero);
void OnTriggerEnter(Collider coll)
{
if(coll.gameObject.tag == "player")
{
thePlayer.transform.SetPositionAndRotation(tpLocation, startRotation);
}
}
*NOTE, YOU WILL HAVE TO ADD A PLAYER TAG TO YOUR PLAYER OBJECT FOR THIS TO WORK.
Oh also, it might help if you make "thePlayer" a GameObject, not a Transform. (I am used to moving game objects to a "Transform".
Thanks for the script @rackomega but it didn’t work, unfortunately. I also made a mistake in the script I posted. I already had thePlayer as a GameObject, not a transform like I posted.
With your script, I added in the line:
Debug.Log(“$$anonymous$$ove $$anonymous$$e!”);
it gives me the message in my console, so I know it’s trying. It also has the look of glitching like it transformed me, then transformed back in a millisecond.
Answer by Kakoha · May 11 at 07:50 PM
The problem here is that auto sync transforms is disabled in the physics settings, so characterController.Move() won't necessarily be aware of the new pose as set by the transform unless a FixedUpdate or Physics.Simulate() called happened in-between transform.position and CC.Move().
To fix that, either enable auto sync transforms in the physics settings, or sync manually via Physics.SyncTransforms right before calling Move().
it resolved the problem for me !