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 () {

}

}

@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…

70801-tele1.png

Note that the destination of TeleportPad1 is Destination2…

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

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.