Open/Close Door only if player is near the Door
Hi I have an Script in which the "Player" can open and close door but he can do it wherever he is. Also a Text ("Press E to Open Door"/"Press F to close Door" appears wherever the player is. What I need help is how can I do that the Player only can open/close the door when he is near it.(also the text). I have an Empty Gameobject (this one has the Script) which is the Hingle and a cube as the door. using UnityEngine; using System.Collections;
public class doorOpenandClose : MonoBehaviour {
//Text variables
public bool T_ActivatedOpen = true;
public bool T_ActivatedClose = false;
public bool activateTrigger = false;
public GameObject textO;
public GameObject textC;
//Animator variables
Animator animator;
bool doorOpen;
//Sound variables
public GameObject doorOpenSound;
public GameObject doorCloseSound;
void Start () { //what happens in the beginning of the game.
textC.SetActive (false);
textO.SetActive (false);
T_ActivatedOpen = true;
T_ActivatedClose = false;
animator = GetComponent<Animator> ();
doorOpen = false;
doorCloseSound.SetActive (false);
doorOpenSound.SetActive (false);
}
void Update () { //The main function wich controlls how the system will work.
if (T_ActivatedOpen == true)
T_ActivatedClose = false;
else if (T_ActivatedClose == true)
T_ActivatedOpen = false;
if (activateTrigger && Input.GetKeyDown (KeyCode.E)) //For some reaseon you can't have both E (open and close).
{
T_ActivatedOpen = false;
T_ActivatedClose = true;
textO.SetActive (false);
textC.SetActive (true);
doorOpen = true;
doorOpenSound.SetActive (true);
doorCloseSound.SetActive (false);
if (doorOpen)
{
doorOpen = true;
doorController ("Open");
}
}
else if(T_ActivatedClose && activateTrigger && Input.GetKey (KeyCode.F))
{
T_ActivatedOpen = true;
T_ActivatedClose = false;
textO.SetActive (true);
textC.SetActive (false);
doorCloseSound.SetActive (true);
doorOpenSound.SetActive (false);
if (doorOpen)
{
doorOpen = false;
doorController ("Close");
}
}
}
void OnTriggerEnter(Collider col) //If you enter the trigger this will happen.
{
if(col.gameObject.tag == "Player")
{
activateTrigger = true;
if((T_ActivatedOpen == true))
textO.SetActive (true);
if((T_ActivatedClose == true))
textC.SetActive (true);
}
}
void OnTriggerExit(Collider col) //If you exit the trigger this will happen.
{
if(col.gameObject.tag == "Player")
{
textO.SetActive (false);
textC.SetActive (false);
activateTrigger = false;
}
}
void doorController(string direction) //Animator function.
{
animator.SetTrigger(direction);
}
}
Answer by alexanderameye · Apr 19, 2017 at 03:12 PM
To check whether or not the player is close to the door, you have two possible situations.
1) You want the text to show up when the player is close to the door
solution: Build an empty gameobject, add a collider to it, mark it as 'IsTrigger' and add a script onto it with OnCollisionEnter and/or OnCollisionStay functions in it. Then you can check if the player is close to the door (has collided with the collider) and open/close the door.
2) You want the text to show up when the player is looking at the door
solution: Add a script onto your player that has a RayCast function and check that when the ray hits a collider, that collider has a certain tag like 'Door', and only then show the text.
If you would like to see some code examples of this, take a look at this free package: https://www.assetstore.unity3d.com/en/#!/content/38694
Good luck,
Alex