public Transform[] Teleporter;
public Player player;
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Player")
{
player.transform.position = Teleporter[Random.Range(0, Teleporter.Length)].transform.position;
}
}
Now I enter the game object which i set as a teleporter to teleport me to somewhere on multiple locations. But the problem is when i enter the collider it will automatically continuously teleport me non stop since I’m always within the collider range all the time. How do i set the distance between the collider and the player whenever I traverse through the “teleporter”? so i won’t have that continuously teleport
One way is to have your teleporters consist of two GameObjects, set slightly-apart from each other. When you teleport, you don’t go to the Teleporter but rather its child, perhaps marked with a custom Monobehavior that you can find with GetComponentsInChildren() off the teleporter itself. This has the added benefit of also letting you specify a custom orientation for this child object, so that the player is set to respawn facing away from the teleporter, which is usually more helpful than either facing randomly, or else facing the teleporter.
Another way is to implement a range check on the teleporter that checks range to player and uses that to decide if you teleport, rather than the triggering. That would let you say “only trigger the teleporter if you come within range AND last frame you were not in range.”
You can also use OnTriggerExit() to force the player to exit the teleporter before re-enabling its teleporter feature, again with a teleporter-specific boolean that inhibits teleporting.
i did that actually hahaha but it didn’t work as what i expected it. I mean it works till i used all of the teleport and it goes back to that non stop teleportation again. Did i do anything wrong? also it didn’t go in a random order
public Transform[] Teleporter;
public Player player;
public bool teleport;
private void OnTriggerEnter(Collider other)
{
if (teleport)
{
if (other.gameObject.tag == "Player")
{
player.transform.position = Teleporter[Random.Range(0, Teleporter.Length)].transform.position;
teleport = false;
}
}
}
private void OnTriggerExit(Collider other)
{
if(other.gameObject.tag == "Player")
{
teleport = true;
}
}
The above certainly SEEMS right, but it will be some subtle misbehavior as far as timing.
I recommend putting Debug.Log() statements in each place that you set and test the teleport boolean to try and figure out the order in which things are being called. I would do it like this and make all your teleport names unique:
Debug.Log( "my name is " + name + " and teleport is now " + teleport.ToString());
That way you can tell at a glance in the log who is becoming true and false. Printing the name of GameObjects is SUPER-useful when debugging with multiple copies of the same type of thing: multiple teleporters, multiple enemies, etc. Obviously, make the names all unique!