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.
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); } } }
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);
}
}