Teleportation, is this the right method?

Hey all, I’ve just though of an idea of laying teleportation pads around the map and actually teleporting the player to the destination. I’ve got this so far.

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

public class TeleportPad : MonoBehaviour
{
    [SerializeField]
    float disableTimer = 0;

    [SerializeField]
    GameObject destPad;

    private void Update()
    {
        if(disableTimer > 0)
        {
            disableTimer -= Time.deltaTime;
        }
    }
    private void OnTriggerEnter(Collider other)
    {
        Debug.Log("Collided");
        if(other.gameObject.tag == "Player" && disableTimer <= 0)
        {
            Debug.Log("Player Collided");
            disableTimer = 2;
            Vector3 position = destPad.transform.position;

            StartCoroutine(ExecuteAfterTime(1.2f));
            other.gameObject.transform.position = position;
        }
    }

    IEnumerator ExecuteAfterTime(float time)
    {
        yield return new WaitForSeconds(time);
    }
}

Nothing seems to be happening, is this the right method for me to use?

Define nothing is happening. Is your console showing the messages “Collided” and “Player Collided”?

Either way, that Coroutine does not what you think it does. It starts, executes nothing, waits 1.2 seconds, executes nothing and then ends. If you want your teleportation to happen after some time, that needs to happen inside that coroutine. Coroutines are not like thread.sleep, pausing the current execution. If it did that would be bad, since Unity only runs on one thread by default, so everything would just freeze^^
Instead Unity keeps track of Coroutines on the mainthread and each frame checks if a coroutine should be executed now, or not. It is simply a convenient way to have something run outside the main Update logic, but it still runs on the same thread. So coroutines run concurrent on the main thread, but not parallel to the main thread.

Other than that, to answer your main question, the general idea of your approach is correct. Have a destination position, and when the player touches your teleport pad, set his position to the destination. Plus some nice animations and delays, if wanted, but thats the core idea.
( Off-Topic: Technically every position change is a teleportation in games. We use things like deltaTime to smooth out movement to appear as if movement happens ‘between frames’, but in the end that is just a trick, and we simply teleport the objects to the correct locations each frame with no state existing inbetween.)

1 Like

Yeah Sorry about that, I was a bit rushed when I wrote it. What your saying is correct, It doesn’t debug anything.

Yeah I was pretty sure that was not gonna run, but still gave it a shot? So do you think I should create a Coroutine do the the teleportation, then start that coroutine in OnTriggerEnter?

Thansk for confirming that. But if my approach is correct, it seem’s right to me. Why doesn’t it work then. My main idea is that when the player goes onto the pad (A cylinder) It’ll wait 1.2f seconds or whatever. Then it’ll teleport. What I’ve done should work, No errors in the console, assigned everything. As well as I have ticket the box for ‘IsTrigger’ on the colliders.

Well, does your player have a rigidbody component attached? Without a rigidbody there are not physics callbacks.

Yes it does, I just watched a little tutorial to give me a hand it helped me out. Thanks for your help anyways!