- Home /
How come the Trigger teleports when its not supposed to!
I have just started unity and have spent HOURS trying to write some code to respond my character when he falls off the side of the platform of my world. I found out how to teleport the player to where I need it to be but it brings the entire trigger box with him! So when my character falls off the world it activates the trigger box and gets teleported back to spawn but the trigger box gets teleported also, so if the character were to jump you would be instantly teleported back to spawn. I have spent so long trying to figure this out and would really appreciate a response.
using UnityEngine; using System.Collections;
public class Spawn : MonoBehaviour {
void OnTriggerEnter (Collider other)
{
//test if this works
Debug.Log ("Entering the zone...");
//If object entering has the tag "player"
if (GameObject.FindWithTag ("Player"))
{
//test if i got this far
Debug.Log ("you got this far...");
//teleports the player to the location, (also teleporting the trigger and the object [MY PROBLEM!])
transform.position = new Vector3 (0, 2, 0);
}
}
}
Answer by abhi_360 · May 22, 2015 at 11:20 AM
Welcome to Unity there is problem with the code -> Attach your script onto your trigger box which i think is below the ground level
void OnTriggerEnter(Collider other)
{
if(other.tag=="Player")
other.transform.position = new Vector3(0,2,0);
}
Remember this only works when your trigger box is below the ground and its size is greater than the ground in the x and z axis.
Your answer
Follow this Question
Related Questions
Do 2 Triggers Collide? 1 Answer
OnTriggerEnter Collision 1 Answer
Ignore collision on tag not working 2 Answers
Tag Player in VRTK 1 Answer