Teleport player script

Hey everyone,

I need a script that when any object touches my “teleporter” they are teleported to another location. I’m using photon and the players are made on runtime. (In other words the players don’t have there own gameobject, when they start the game the object is made.)

Thanks.

var targetPosition : Vector3;

function OnTriggerEnter( other : Collider ) {
  other.transform.position = targetPosition;
}

If Loius’ answer doesn’t work for you, make sure the object you’re trying to teleport has a PhotonView and a PhotonTransformView attached to it. And make sure you add the PhotonTransformView to the PhotonView’s Observed Components list.

135677-unity-photon-question-teleport-transform.png

If that doesn’t work, or you also have to sync your Rigidbody2D, you can add a PhotonRigidbody2DView to your object in the same way.
135678-unity-photon-question-teleport-rigidbody2d.png

If it still doesn’t work, try replacing gameObject.transform.position = targetPosition; with a call to teleportObject():

IEnumerator teleportObject(GameObject go, Vector3 newPosition)
{
    Rigidbody2D rb2d = go.GetComponent<Rigidbody2D>();
    rb2d.isKinematic = true;
    while (go.transform.position != newPosition)
    {
        go.transform.position = newPosition;
        yield return null;
    }
    rb2d.isKinematic = false;
    yield return null;
}

You can call this method with:

 StartCoroutine(teleportObject(gameObject, targetPosition);

You might also be able to get away with just setting Rigidbody2D.isKinematic = true; before setting the position and then just setting it back with Rigidbody2D.isKinematic = false; at the end, but I haven’t tested it.