- Home /
Teleporting Player to a point using box colliders
I got some problem with teleporting my character to a certain point.
var TelePosition : Vector3;
function OnTriggerEnter(other : Collider)
{
if (other.tag == "Player")
{
transform.position = TelePosition;
}
}
// Box Collider Is Trigger must be checked to work.
// Change the transform.position coordinates in the Inspector.
This is the code i wrote. The idea is that if my player falls off the map and collide with the box collider, the player gets respawned or teleported to a different location that i have given with var TelePosition : Vector3;
Now the problem is that when collide, the Player don't get teleported to that location. Instead the Box Collider decide it can teleport to that point.
*notice that i already tag my character with the tag : Player
So any idea what the problem is? I appriciate some help :)
Answer by EHogger · Aug 02, 2013 at 11:47 AM
transform.position
is the position of the object your script is attached to, which in this case is your box. Change it to other.transform.position = TelePosition
and it will move the object that hits it instead (the player).
That's right. And it would be better to define the destination point with an empty game object ins$$anonymous$$d of a Vector3: it's easier to set the position in the Editor, and you can define the rotation as well:
var TelePosition : Transform; // drag the destination empty here
function OnTriggerEnter(other : Collider)
{
if (other.tag == "Player")
{
// move and align the player to the destination empty GO
other.transform.position = TelePosition.position;
other.transform.rotation = TelePosition.rotation;
}
}
This avoids the embarrasing situation where the player gets teleported to the destination point but keeps looking in the old direction.
Thanks guys, both of you. I already thought i was missing something but couldn't put my finger on it.
The idea to put an empty game object ins$$anonymous$$d of just a Vector3 coordinate is a smart idea, so i could just drag or change the position. I didn't really need to change the rotation of the object because i let my character respawn in the middle of the map without walls or objects around it that could probably hinder the player.
I will re$$anonymous$$d it for my next project, probably usefull in a teleport game :).
Your answer
Follow this Question
Related Questions
Game Object won't match rotation of new positions transform 1 Answer
Making The Hole In A Golf Game 1 Answer
Teleportation Loop issue 1 Answer
Transform position? 2 Answers