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 () {
}
}
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.
Does any of these objects have a rigidbody? Do both of them have colliders? Are you sure the teleport object is tagged with "Teleport" ?
@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 ;)
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...
Note that the destination of TeleportPad1 is Destination2...
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.
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).
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
go to "Project Settings > Physics" and make sure "Auto Sync Transformations" is checked.
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.
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;
}
}
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.