Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by AusAndrew19 · May 23, 2012 at 10:44 AM · teleport

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

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

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hathol · May 23, 2012 at 11:31 AM 1
Share

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 :)

avatar image AusAndrew19 · May 23, 2012 at 11:32 AM 0
Share

Thank you mate. testing this now :)

avatar image AusAndrew19 · May 23, 2012 at 11:52 AM 0
Share

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.

avatar image AusAndrew19 · May 23, 2012 at 12:58 PM 0
Share

$$anonymous$$y audio clip is called "tele" if thats anyhelp.

avatar image aldonaletto · May 23, 2012 at 01:04 PM 0
Share

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

Show more comments
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges