- Home /
Teleport to location help
Im wanting to make it so that when some one presses the "E" button it actaves a teloport to a specific loation.
The item im want to teleport will be called ball I have found this code but im not quite shure how to work from it to get it to work.
var teleportTo : Transform;
function OnCollisionEnter (col : Collision) {
col.transform.position = teleportTo.position;
}
Answer by aldonaletto · Nov 15, 2011 at 05:06 PM
This code is intended to be used in a "portal" object: when other object touches the portal (to which this code is attached), it's teleported to the teleportTo.position. It would be better to use a trigger for the portal, and OnTriggerEnter(col: Collider) instead.
Anyway, this will not solve your problem. The code you need is something like this (attach this code to the ball):
// create an empty object in the teleport destination and drag it to this variable: var teleportTo: Transform;
function Update(){ if (Input.GetKeyDown("e")){ transform.position = teleportTo.position; // move the ball to the destination transform.rotation = teleportTo.rotation; // make it face the dest forward direction } } Maybe this rotation thing isn't needed for a ball, but for most characters it's important to appear in the destination always facing the same side.
NOTE: Place the empty destination object at some height above the ground: the ball will fall from this position, which is cool, and you will avoid weird effects if the destination position is too low (like the character falling through the floor - or being ejected to some random direction, if it's a rigidbody).
Your answer

Follow this Question
Related Questions
Disable/Enable Colliders 1 Answer
GUI text Stays in place 1 Answer
Grab/Throw Parenting 1 Answer
Popup on collider 1 Answer
PinBall Flippers 1 Answer