Teleport strange issue

Hey guys,

I know different questions have already been asked concerning teleport scripts in Unity. Well, I want to teleport my player from a location to an other one.

Everything SHOULD work, I wrote the script that change OnTriggerEnter the position of the player, attached it on the Cube which is currently the trigger to teleport and I inserted in the editor the “targetPosition”, the position of my GameObject where I should teleport :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Teleport : MonoBehaviour
{
    public Transform teleportTarget;
	public GameObject ThePlayer;
	
	void OnTriggerEnter(Collider col)
	{
		Debug.Log("Actual Player pos : "+ThePlayer.transform.position);
		Debug.Log("Position targeted : "+teleportTarget.transform.position);
		ThePlayer.transform.position = teleportTarget.transform.position;
		Debug.Log("Teleported");
		Debug.Log("Actual Player pos : "+ThePlayer.transform.position);
		Debug.Log("Position targeted : "+teleportTarget.transform.position);
	}
	
}

Well, since it’s not teleporting my player, I just tried to check the position of the player before triggering the cube and the position targeted, then the position reached, and it SEEMS that it should work, but it’s nnot though.

Do you guys have any thought on that ?

Thank you so much for your help

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

 public class Teleport : MonoBehaviour
 {
     public Transform teleportTarget;
     public GameObject ThePlayer;
     
     void OnTriggerEnter(Collider col)
     {
         if(col.gameObject.CompareTag("player") { //Note: this will only work if the tag of the player is set to "player", change this to whatever tag you are using
         Debug.Log("Actual Player pos : "+ThePlayer.transform.position);
         Debug.Log("Position targeted : "+teleportTarget.transform.position);
         ThePlayer.transform.position = teleportTarget.transform.position;
         Debug.Log("Teleported");
         Debug.Log("Actual Player pos : "+ThePlayer.transform.position);
         Debug.Log("Position targeted : "+teleportTarget.transform.position);
          }
     }
     
 }

Hello @unity_gOBubnjTkYJDwA !
I had a similar issue some time ago. I was using the FPSController prefab from Standard Assets, and disabling the FirstPersonController script attached to it and enabling it after changing position worked for me. So something like this (if you are also using the FPSController):

    ThePlayer.GetComponent<FirstPersonController>().enabled = false;
    ThePlayer.transform.position = teleportTarget.transform.position;
    ThePlayer.GetComponent<FirstPersonController>().enabled = true;

Hi, it’s good that you have already debugged your script to make sure that it executes and have right variables. However, since it still not working I would suggest you to step away from OnTriggerEnter and start to test out in Update mode by simply trying to teleport your Player with any input, for example Input.GetKeyDown(KeyCode.T).

By doing that you will give yourself option to enable-disable components on your Player and try to teleport with Input after each adjustment you made in inspector. Another good way to test things out is to try execute your code by using other GameObject. Create Cube and try to teleport it with your script - it should work since you already debugged it and there is actually no issue with your code.

By isolating this way you will find what component in your scene are preventing your Player from teleporting. Keep in mind that if you will find yourself in situation where you have already disabled all components on your Player but it still won’t teleport, but the empty Cube will - you should check out your other scripts, because it looks like something is caching and updating your Player position. Hope that will help