Teleportation Loop issue

void OnTriggerEnter(Collider other){
if (other.transform.tag == “Teleport 1”) {
transform.position = teleport2.transform.position;
}
if (other.transform.tag == “Teleport 2”) {
transform.position = teleport1.transform.position;
}
}

So, now the problem is that the object (player) gets teleported from 1 position to the other very fast, infinitely (each frame).

Is there any way to stop the teleportation from happening until player leaves the trigger?

By the way, teleport1 and 2 are GameObject variables defined above the OnTriggerEnter function.

Thanks! :slight_smile:

There are a number of ways; you could add a boolean

private bool justTeleported = false;

and modify this to

    void OnTriggerEnter(Collider other){
    if (!justTeleported && other.transform.tag == "Teleport 1") {
    transform.position = teleport2.transform.position;
justTeleported = true;
    }
    if (!justTeleported && other.transform.tag == "Teleport 2") {
    transform.position = teleport1.transform.position;
justTeleported = true;
    }

then add

OnTriggerExit(...)
justTeleported = false;
    }