- Home /
Teleport in a certain area to another area by using button.
I want to ask is that possible to teleport in a area to another area by using button.
Example I set a button which is keyboard E, when i press E in a area it will teleport me to another area.
this is my code.
private Transform player;
void Awake()
{
player = GameObject.FindGameObjectWithTag ("Player").transform;
}
void Update()
{
if (Input.GetKeyDown (KeyCode.E) )
{
player.position = warp1.position;
}
}
this code giving me when i press E it direct teleport me to other area even no in the area that i want.
Comment
I mean the code teleport me to other area in any location, but i want is teleport me by using button E in a certain area only.
Answer by toddisarockstar · Aug 19, 2017 at 08:02 PM
you could make an empty game object in the middle of your start area and name it "marker". then simply check the distance from it.
public Transform player,startarea,warpto;
public float areasize = 5;
void Start () {
player = GameObject.Find ("Player").transform;
startarea = GameObject.Find ("marker").transform;
warpto = GameObject.Find ("warp").transform;
}
void Update () {
if(Input.GetKeyDown("e")){
if(Vector3.Distance(player.position,startarea.position)<areasize){
player.transform.position=warpto.position;
}}}
Your answer