- Home /
Opening door with the same key?
Hey guys, i am struggeling a bit with opening doors with the same key.
For now im using a script that opens with E and closes with F. It also contains sound and text on trigger. Any ideas what im missing?
//Script made by Strups Productions// 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).
{
textO.SetActive (false);
textC.SetActive (false);
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.GetKeyDown(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 Spidlee · Mar 22, 2017 at 02:39 AM
I believe you have an error in your logic handling. You shouldn't need to use different keys to open / close a door.
Very roughly this is what I would do:
bool isDoorOpen = false;
public TextMesh keyPressPrompt;
string s_actionKey = "Press E";
string s_openDoor = "Open Door";
string s_closeDoor = "Close Door";
void OpenCloseDoor ()
{
// handle relevant open / close door animations
if(isDoorOpen)
{
// close door animation
}
else
{
// open door animation
}
// toggle door open state
isDoorOpen = !isDoorOpen;
}
void KeyPressPrompt (bool activate)
{
if(!activate)
{
// handle deactivation animation etc
}
else
{
// handle activation animation etc
// show Close Door Prompt if Door is Open, vice versa
keyPressPrompt.text = (isDoorOpen ? s_closeDoor : s_openDoor) + "\n" + s_actionKey;
}
}
void CheckForKeyInput ()
{
if(Input.GetKeyDown(KeyCode.E))
OpenCloseDoor();
}
void Start ()
{
// init stuff
}
void Update ()
{
CheckForKeyInput();
}
void OnTriggerEnter (Collider col)
{
if(col.gameObject.tag == "Player")
KeyPressPrompt(true);
}
void OnTriggerExit (Collider col)
{
if(col.gameObject.tag == "Player")
KeyPressPrompt(false);
}
Hope that helps get you on the right direction.
Thanks alot man! I was able to make it work!
Also one more question if i could bug you further.. Any ideas how to add audio to the scene? Two clips, one for open and one for close? Tried many combinations, but got into trouble when player collides with door. It then triggers sound as well as when i open door.
Not really sure I understand your question.
But to handle playing the right audio clip, I would just play the appropriate audio clip in OpenCloseDoor(), using if(isDoorOpen) to deter$$anonymous$$e which to play.
What I usually do for a more indepth solution would be to create an Audio$$anonymous$$anager, when I organise all possible audio clips to be played in the game, and call the relevant functions to play what I need.