- Home /
Stuck in Teleporter Loop
I got a teleporting script in C# from another user here...... it works except that i have been stuck in an endless loop of telporting back and forth
i have figured i need make the script pause long enough to let the player exit the trigger area so that it will stop it from looping endlessly
How am I to make this script pause itself?
using UnityEngine;
using System.Collections;
public class teleporter : MonoBehaviour
{
//this is the exit object
public Transform exit;
//This is the player
public Transform TeleportMe;
//teleport if in the teleporter this way, because 2D rigid bodies weren't returning trigger
void OnTriggerEnter2D(Collider2D other)
{
TeleportMe.position = exit.position;
}
}
i tried this but it still doesnt work.....i dont know what else i can do
using UnityEngine; using System.Collections;
public class teleporter : $$anonymous$$onoBehaviour
{
public Transform exit;
public Transform Teleport$$anonymous$$e;
private bool justTP;
void OnTriggerEnter2D(Collider2D other)
{
if (justTP == false){
Teleport$$anonymous$$e.position = exit.position;
justTP = true;
}
}
void OnTriggerStay2D(Collider2D other)
{
if (justTP == false){
Teleport$$anonymous$$e.position = exit.position;
justTP = true;
}
}
void OnTriggerExit2D(Collider2D other)
{
if (justTP){
Teleport$$anonymous$$e.position = exit.position;
justTP = true;
}
}
}
In "OnTriggerEnter2D", set "exit.getComponent<teleporter>().justTP = true". (You'll may need to set "justTP" to a public or protected variable.)
Leave the "OnTriggerExit2D" alone. I don't think you'll need the "OnTriggerStay2D" function at all.
"endless loop of teleporting back and forth", but how? When you have only destination where to go and not where to back...
Are you using this object for entrance and for exit as two separate objects? And "justTP" has no false state in your script so "function OnTriggerStay2D" will never work.
Answer by joseaguirre · Jun 15, 2015 at 10:57 PM
using UnityEngine;
using System.Collections;
public class teleporter : MonoBehaviour
{
public Transform exit;
public Transform TeleportMe;
private static bool justTP;
void Awake(){
justTP = false;
}
IEnumerator OnTriggerEnter2D(Collider2D col)
{
if (!justTP){
justTP = true;
TeleportMe.position = exit.position;
yield return new WaitForSeconds(0.5f);
justTP = true;
}
}
IEnumerator OnTriggerExit2D(Collider2D col)
{
if (justTP){
justTP = true;
yield return new WaitForSeconds(0.5f);
justTP = false;
}
}
}
$$anonymous$$ADE IT WOR$$anonymous$$ WITHOUT YOU GUYS YAY! im proud of myself
Answer by William_Lee_Sims · Jun 16, 2015 at 05:44 AM
Typically for these types of problems you need to do one of two things. Either:
A) Create a boolean on the player that prevents him from teleporting, set it before the teleport, test it in the OnTriggerEnter, and unset it when the player leaves the trigger.
or
B) Create a boolean on the teleporter that prevents any teleporting through it while somebody is in it, set it on the exit teleporter when a player uses a teleporter, test it in the OnTriggerEnter, and unset it when the OnTriggerExit is called.