- Home /
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! :)
Answer by getyour411 · Apr 05, 2014 at 06:30 PM
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;
}
Wow, makes a man think how did he not think of that... Especially since I'm an avid boolean user (as a main c++ student programmer)
That will work! Thanks! ^^
Your answer
Follow this Question
Related Questions
Making The Hole In A Golf Game 1 Answer
Teleporting Player to a point using box colliders 1 Answer
Teleport object to other objects position 2 Answers
Transform.Position Help 3 Answers
Transform Position Not Moving Object 1 Answer