I have created a key and door. The door only opens when the key is obtained by the player. The player has to press “E” to obtain the card. When the player presses “E”, I want a sound played. My script isn’t working so I was hoping someone could be able to help.
#pragma strict
var TheKey : GameObject;
private var playerNextToKey = false;
var KeySound : GameObject;
function Update ()
{
if (Input.GetKeyDown(KeyCode.E) && playerNextToKey == true)
{
TheKey.active = false;
}
}
function OnTriggerEnter (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
playerNextToKey = true;
KeySound.audio.Play();
}
}
function OnTriggerExit (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
playerNextToKey = false;
KeySound.audio.Play();
}
}