- Home /
Teleport Help.
if i wanted to add a script to a character so when i press say... "T" It will teleport me forward about 20ft. Similar to World Of Warcraft Mage Blink. How would i go about doing this? I Couldnt find any tuts around this or any info at all to be honest so all the help is appreciated.
Answer by aldonaletto · May 23, 2012 at 11:12 AM
Doing the teleport is the easiest part (character script):
var distance: float = 20;
function Update(){ if (Input.GetKeyDown("t")){ transform.position += transform.forward distance; } } You can add the if* to an existing Update in some character script, or make this a new script and attach it to the character (negligibly slower alternative).
The big problem with this kind of teleport is that your character may be teleported to inside some object (a rock, a building, a wall etc.) or, even worse, be teleported to a higher spot in the terrain, what will make it fall through the floor for the eternity.
The simplest way to avoid this is to calculate the new position and do a raycast downwards, placing the character at a safe height over any collider that may exist in the target point:
var distance: float = 20;
function Update(){ if (Input.GetKeyDown("t")){ var dest = transform.position + transform.forward * distance; dest.y = 1000; // choose a really high spot to do the raycast var hit: RaycastHit; if (Physics.Raycast(dest, -Vector3.up, hit)){ // if there's ground below the destination point... dest = hit.point; // define the destination somewhat above the hit.point dest.y += 2; // to make the character fall to the ground transform.position = dest; // you can play some "teleport sound" here, if you want, with audio.PlayOneShot(someAudioClip); } } } It's important to teleport the character to a point above the ground: this prevents it to fall through the terrain, and the result is nicer than simply placing the character there.
EDITED:
1- To add the sound, just replace the comment line with audio.PlayOneShot(teleportSound), declare an AudioClip variable and fill it in the Inspector (your character must have at least one AudioSource component attached, of course).
2- This code teleports the player over the obstacles (building, walls etc.) if there's any one at the destination point. @hathol's suggestion is good to ensure the teleport happens before an obstacle , and could be implemented this way:
var teleportSound: AudioClip; // assign the sound in the Inspector var distance: float = 20;
function Update(){ if (Input.GetKeyDown("t")){ var dest = transform.position + transform.forward distance; var hit: RaycastHit; // if some obstacle between player and dest... if (Physics.Linecast(transform.position, dest, hit)){ // set dest to 1.5m before the obstacle dest = transform.position + transform.forward (hit.distance - 1.5); } dest.y = 1000; // choose a really high spot to do the raycast if (Physics.Raycast(dest, -Vector3.up, hit)){ // if there's ground below the destination point... dest = hit.point; // define the destination somewhat above the hit.point dest.y += 2; // to make the character fall to the ground transform.position = dest; audio.PlayOneShot(teleportSound); } } }
You could also do a raycast along transform.forward with your max teleport distance first to check if there is an object in the way. If that's the case, lower the teleport distance to hit.distance $$anonymous$$us some threshold (so your character doesn't end up halfway stuck in the wall). Aside from that: what Aldo said :)
This Works 100%, Great work thank you so much mate. I was also wondering where do i add the
audio.PlayOneShot(someAudioClip);
Do you know a way to add FX to it when i press T say i wanted to add some smoke when i click it :) Thank you again.
$$anonymous$$y audio clip is called "tele" if thats anyhelp.
To add the sound, just replace the comment line with audio.PlayOneShot(teleportSound), declare an AudioClip variable and fill it in the Inspector (your character must have at least one AudioSource component attached, of course):
var teleportSound: AudioClip; // assign the sound in the Inspector var distance: float = 20;
function Update(){ if (Input.Get$$anonymous$$eyDown("t")){ ... transform.position = dest; audio.PlayOneShot(teleportSound); } } } This code teleports the player over the obstacles (building, walls etc.) if there's any one at the destination point. @hathol's suggestion is good to ensure the teleport happens before an obstacle , and could be implemented this way:
var teleportSound: AudioClip; // assign the sound in the Inspector var distance: float = 20;
function Update(){ if (Input.Get$$anonymous$$eyDown("t")){ var dest = transform.position + transform.forward distance; var hit: RaycastHit; // if some obstacle between player and dest... if (Physics.Linecast(transform.position, dest, hit)){ // set dest to 1.5m before the obstacle dest = transform.position + transform.forward (hit.distance - 1.5); } dest.y = 1000; // choose a really high spot to do the raycast if (Physics.Raycast(dest, -Vector3.up, hit)){ // if there's ground below the destination point... dest = hit.point; // define the destination somewhat above the hit.point dest.y += 2; // to make the character fall to the ground transform.position = dest; audio.PlayOneShot(teleportSound); } } }
Answer by epicjosh · Sep 01, 2016 at 11:24 PM
Found a way to convert it to c#... If anyone wants it
public float distance = 5.0f;
public AudioClip blinkSound;
void Update()
{
if (Input.GetKeyDown("t"))
{
Blink ();
}
}
void Blink()
{
Vector3 dest = transform.position + transform.forward * distance;
RaycastHit hitting;
if (Physics.Linecast (transform.position, dest, out hitting))
{
dest = transform.position + transform.forward * (hitting.distance - 1.5f);
}
dest.y = 1000;
if (Physics.Raycast (dest, -Vector3.up, out hitting))
{
dest = hitting.point;
dest.y += 2;
transform.position = dest;
AudioSource.PlayClipAtPoint(blinkSound);
}
}
Your answer

Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Script to teleport player (Beginner here) 0 Answers
how to add a sound to this script 3 Answers
teleporting player after time runs out 2 Answers
Infinity Hallway 1 Answer