Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Hankyana · May 24, 2016 at 05:11 PM · c#teleport

Teleport Script

So, I made a teleport script but I'm not sure if it works, or if I am using the script wrong, if I the code is right, may I get a tutorial on how to initialize the code properly? I've been putting my teleport exit gameobject on the destination obj but it hasn't beeen working.

using UnityEngine; using System.Collections;

public class GameRules : MonoBehaviour {

 public Transform destination;
 // Use this for initialization
 void Start () {
 
 }
 public void OnTriggerEnter2D(Collider2D other)
 {
     if(destination.tag.Equals("Teleport") && other != null)
     {
         transform.position = destination.position;
     }
 }
 // Update is called once per frame
 void Update () {
 
 }

}

Comment
Add comment · Show 4
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 Mmmpies · May 24, 2016 at 05:25 PM 0
Share

Don't you mean other.tag rather than destination.tag?

avatar image Hankyana Mmmpies · May 25, 2016 at 04:40 PM 0
Share

So I changed it to other.tag, and set the Teleport gameobj to be a trigger, set the TeleportExit as
the destination, but it won't work yet. I'm not sure what exactly is wrong and I'm considering writing a new script.

avatar image JedBeryll Hankyana · May 25, 2016 at 08:01 PM 0
Share

Does any of these objects have a rigidbody? Do both of them have colliders? Are you sure the teleport object is tagged with "Teleport" ?

avatar image DiGiaCom-Tech · May 26, 2016 at 02:52 PM 0
Share

@Hankyana ... First, your code is checking the destination's tag which is not necessary. Assu$$anonymous$$g you are setting this in the Inspector only you should be selecting a correct/proper destination gameObject so checking is not necessary.

Second, the other.tag could be anything that is in a collision with this gameObject. If you want to restrict who/what gets teleported (i.e. players & bosses can but regular enemies can't) then you need to compare the other.tag to a list of acceptable tags.

Lastly, as others have pointed out, you should be moving the other gameObject not the object containing the code/collider.

I have written up a base class (see below) that will get this done for you. $$anonymous$$ake sure you set things up as indicated and you should be fine ;)

3 Replies

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

Answer by DiGiaCom-Tech · May 25, 2016 at 09:08 PM

@Hankyana ... I am assuming the Player (P) is coming in contact with a Collider (C) of some kind and is then instantly teleported to Destination (D).

Here is my code (written for 3D but you can easily change it to 2D)...

 using UnityEngine;
 using System.Collections;
 
 public class Teleport : MonoBehaviour {
 
     public Transform Destination;       // Gameobject where they will be teleported to
     public string TagList = "|Player|Boss|Friendly|"; // List of all tags that can teleport
 
     // Use this for initialization
     void Start () {
         // As needed
     }
     
     // Update is called once per frame
     void Update () {
         // As needed
     }
 
     public void OnTriggerEnter(Collider other)
     {
         // If the tag of the colliding object is allowed to teleport
         if (TagList.Contains(string.Format("|{0}|",other.tag))) {
             // Update other objects position and rotation
             other.transform.position = Destination.transform.position;
             other.transform.rotation = Destination.transform.rotation;
         }
     }
 }

Note that I am checking to see who is allowed to use the teleporter. If you want to allow others through then add their tag names to the list (delimited by pipes |). Also note that the destination is a separate object that needs to be placed away from the collider (and above the ground so the player won't fall through it).

This code is placed on the source teleport object not the destination object. No code needs to be placed on the player or other objects that can use the teleports.

Here is the setup...

alt text

Note that the destination of TeleportPad1 is Destination2...

alt text


tele2.png (72.3 kB)
tele1.png (162.9 kB)
Comment
Add comment · Show 5 · 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 Iman_col · Apr 14, 2018 at 02:47 AM 0
Share

@DiGiaCom-Tech

Hi. When I insert your Script I do not get the public classes where I insert the Script, rather it says an error when loading it. I use the Unity version 2017.4.1f1 (64-bit). I guess this is for Unity 5. Is there a way to update the code ?. Thank you very much.

avatar image DiGiaCom-Tech Iman_col · Apr 14, 2018 at 01:42 PM 0
Share

I installed Unity 2017 and tested the my code and am not getting any errors. So at this point, either send your error messages to me (so I can see that is wrong) and/or some screen grabs (that show what is wrong) so I can try to figure this out.

Not sure what you mean by 'When I insert your Script' ... the script is not to be inserted into any other scripts ... it is a class by itself.

That is, create a new C# script, name it 'Teleport' (without the single quotes), paste in $$anonymous$$Y code (everything above, all 28 lines, from top to bottom ... NOT the other guy's code), and let it compile. Once Unity has compiled it without errors you can drop it onto your objects and start setting properties.

Remember:

  • Set the destination for TeleportPad 1 to Destination 2 (that is a child of TeleportPad 2).

  • $$anonymous$$ake sure that you set the Colliders of the teleportPads as 'Triggers' (check that box).

avatar image AiDev123 · Jul 27, 2020 at 03:33 AM 0
Share

I know this is kind of old at this point, but I used the script and had no errors but it doesn't teleport the player. I put the code on one cube that was set as a trigger, then set the destination to another cube further away from it and allowed the tag "Player" to teleport. $$anonymous$$y player is tagged player and it just doesn't do anything

avatar image dhend0324 AiDev123 · Jul 30, 2020 at 04:50 AM 0
Share

go to "Project Settings > Physics" and make sure "Auto Sync Transformations" is checked.

avatar image dhend0324 · Aug 02, 2020 at 02:18 PM 0
Share

For those like me where the teleport script didn't move the character, go to "Project Settings > Physics" and make sure "Auto Sync Transformations" is checked.

avatar image
0

Answer by bigboicalvin · Aug 31, 2020 at 10:39 PM

here is updated version that works

 using UnityEngine;
 public class Teleport : MonoBehaviour { 
     public Transform Destination;
     public Transform player;
     public void OnTriggerEnter(Collider other) { 
         player.transform.position = Destination.transform.position;
     }
 }
 

  
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
avatar image
0

Answer by JTAGames · Jul 02, 2021 at 02:59 AM

https://www.youtube.com/watch?v=pNGwPrjnwfo

In this tutorial you can teleport from anywhere, and you won't clip into things. Just in case that's what some people are looking for when they come here.

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

167 People are following this question.

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

Related Questions

Teleport script help! 1 Answer

Is there a way to teleport player after countdown timer hits zero? 1 Answer

Script for teleport on collision? 2 Answers

2D Power up script not working 0 Answers

How to make a Teleporter Pad send Player to other random Teleporter Pad? 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