Skip to main content
Compacted the code.
Source Link
Vaillancourt
  • 16.4k
  • 17
  • 56
  • 61

I made some changes and simplified the code for you. You will need to set the collider on the Teleporter as a trigger and then drag in the object that you want your destination to be as well as your player character.

using UnityEngine; using System.Collections;

public class playerTeleport : MonoBehaviour {

using UnityEngine;
using System.Collections;

public class playerTeleport : MonoBehaviour {

  bool isInRange = false; 
    public Transform destination;
    public GameObject player;
    

    void Update()
    {
        if (isInRange)
        {
            if&& (Input.GetKeyDown(KeyCode.E))
            {
                Teleport();
            }
        }
    }

void OnTriggerEnter (Collider other)
  {
 
    if(other.gameObject == player)
    {
        isInRange = true;
    }
}

  void OnTriggerExit (Collider other)
  {
    if(other.gameObject == player)
    {
        isInRange = false;
    }
}
    

 
    void Teleport()
    {
        player.transform.position = destination.position;
    }
}

I made some changes and simplified the code for you. You will need to set the collider on the Teleporter as a trigger and then drag in the object that you want your destination to be as well as your player character.

using UnityEngine; using System.Collections;

public class playerTeleport : MonoBehaviour {

    bool isInRange = false; 
    public Transform destination;
    public GameObject player;
    

    void Update()
    {
        if (isInRange)
        {
            if (Input.GetKeyDown(KeyCode.E))
            {
                Teleport();
            }
        }
    }

void OnTriggerEnter (Collider other)
{
 
    if(other.gameObject == player)
    {
        isInRange = true;
    }
}

void OnTriggerExit (Collider other)
{
    if(other.gameObject == player)
    {
        isInRange = false;
    }
}
    

 
    void Teleport()
    {
        player.transform.position = destination.position;
    }
}

I made some changes and simplified the code for you. You will need to set the collider on the Teleporter as a trigger and then drag in the object that you want your destination to be as well as your player character.

using UnityEngine;
using System.Collections;

public class playerTeleport : MonoBehaviour {

  bool isInRange = false; 
  public Transform destination;
  public GameObject player;
  

  void Update()
  {
    if (isInRange && Input.GetKeyDown(KeyCode.E))
      Teleport();
  }

  void OnTriggerEnter (Collider other)
  {
    if(other.gameObject == player)
      isInRange = true;
  }

  void OnTriggerExit (Collider other)
  {
    if(other.gameObject == player)
      isInRange = false;
  }
  
  void Teleport()
  {
    player.transform.position = destination.position;
  }
}
Source Link

I made some changes and simplified the code for you. You will need to set the collider on the Teleporter as a trigger and then drag in the object that you want your destination to be as well as your player character.

using UnityEngine; using System.Collections;

public class playerTeleport : MonoBehaviour {

    bool isInRange = false; 
    public Transform destination;
    public GameObject player;
    

    void Update()
    {
        if (isInRange)
        {
            if (Input.GetKeyDown(KeyCode.E))
            {
                Teleport();
            }
        }
    }

void OnTriggerEnter (Collider other)
{

    if(other.gameObject == player)
    {
        isInRange = true;
    }
}

void OnTriggerExit (Collider other)
{
    if(other.gameObject == player)
    {
        isInRange = false;
    }
}
    


    void Teleport()
    {
        player.transform.position = destination.position;
    }
}