Making a better seamless teleport

How do you make a player controller that works with a seamless teleport?

I want to build a better seamless teleport that works with player controller.

I had to enable auto sync transforms in the physics settings because I use character controller with move.

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

public class SeamlessTeleport2 : MonoBehaviour
{
    public Transform Teleport;
    public Transform TeleDest;
    public Vector3 Position;
    public Quaternion Rotation;
    public GameObject Player;

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    void OnTriggerEnter(Collider other)
    {
        Player.transform.parent = Teleport.transform;
        Position = Player.transform.localPosition;
        Rotation = Player.transform.localRotation;
        Player.transform.parent = TeleDest.transform;
        Player.transform.localPosition = Position;
        Player.transform.localRotation = Rotation;
        Player.transform.parent = null;
    }
}

You only need to set the Player.transform.position to your destination’s transform.position.

With the CharacterController component, you need to disable the component before moving the player. You also don’t need to reference the player via the inspector, when you will gain access to it via the OnTriggerEnter callback. As your code is, if anything enters the trigger, the player will be suddenly teleported.

Can be as simple as:

public class Teleporter : MonoBehaviour
{
    [SerializeField]
    private Transform _destination;

    private void OnTriggerEnter(Collider other)
    {
        if (other.TryGetComponent(out CharacterController controller))
        {
            controller.enabled = false;
            controller.transform.position = _destination.position;
            controller.enabled = true;
        }
    }
}

Thanks.

It works much better now.

I could not find a simpler way to get the local position to the other parent game object and then teleport.

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

public class SeamlessTeleport2 : MonoBehaviour
{
    public Transform Teleport;
    public Transform TeleDest;
    public Vector3 Position;
    public Quaternion Rotation;

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.TryGetComponent(out CharacterController controller))
        {
            controller.transform.parent = Teleport.transform;
            Position = controller.transform.localPosition;
            Rotation = controller.transform.localRotation;
            controller.transform.parent = TeleDest.transform;
            controller.enabled = false;
            controller.transform.localPosition = Position;
            controller.transform.localRotation = Rotation;
            controller.enabled = true;
            controller.transform.parent = null;
        }   
    }
}

You don’t need the local position or any of that extra fluff code, you just need the world position. The code I posted will work fine.

This is for endless hallways or teleporting for two identical, but different places.

I’m looking for the position and rotation offset to the teleport destination.

It’s like the landmarks in Half life 2 for teleporting.

Yes, but at the end of the day, your code is just a protracted way of doing:

controller.enabled = false;
controller.transform.position = _destination.position;
controller.transform.rotation = _destination.rotation;
controller.enabled = true;

They do the exact same thing, just yours with a lot of extra steps.

1 Like