How to make this door open once the key has been walked over?
I'm trying to make a door open once the key has been picked up/destroyed. I'm not good with scripted yet so I need some help.
Door Code:
public class OpenCafeteria : MonoBehaviour {
public GameObject Doorknob;
public GameObject Objective2Complete;
public GameObject doorHinge;
private Animator hingeAnim;
private bool isOpen;
public AudioSource controlAudio;
public AudioSource controlAudio2;
// Use this for initialization
void Start()
{
hingeAnim = doorHinge.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
isOpen = hingeAnim.GetBool("toggleOpen");
}
void OnTriggerEnter(Collider entity)
{
if (entity.tag == "Player")
{
print("The Player has entered the door.");
}
}
void OnTriggerStay(Collider entity)
{
if (entity.tag == "Player")
{
if (Input.GetKeyUp(KeyCode.E) && isOpen == false)
{
hingeAnim.SetBool("toggleOpen", true);
controlAudio.Play();
print("The Player opened the door.");
}
if (Input.GetKeyUp(KeyCode.E) && isOpen == true)
{
hingeAnim.SetBool("toggleOpen", false);
controlAudio2.Play();
print("The Player closed the door.");
}
}
}
void OnTriggerExit(Collider entity)
{
if (entity.tag == "Player")
{
print("The Player has left the door.");
}
}
}
And Here is the Key code:
public class Objective2Complete : MonoBehaviour {
public GameObject ObjectiveListManager;
public AudioSource controlAudio;
public bool objTwoComplete = false;
// Use this for initialization
void Start ()
{
}
void OnTriggerEnter(Collider entity)
{
print("Player picked the Cafeteria Key");
if (entity.tag == "Player")
{
objTwoComplete = true;
controlAudio.Play();
Destroy(gameObject);
ObjectiveListManager.GetComponent<ObjectiveList>().CompleteObjective(2);
print("Objective 2 completed");
}
}
// Update is called once per frame
void Update ()
{
}
}
If it's not too much trouble, could you ELI5 why the lines of code you added are needed? So I don't have this problem again in the future.
Does this code successfully make the door open?
if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.E) && isOpen == false)
{
hingeAnim.SetBool("toggleOpen", true);
controlAudio.Play();
print("The Player opened the door.");
}
If so, then you just have to do basically the same thing when the player gets the key. Based on your scripts, you should have all the knowledge you need to do this.
Your answer
Follow this Question
Related Questions
Manual activation error - Hwid ulf can't be blank 0 Answers
I have a error in a simple script 0 Answers
S key always pressed 0 Answers
How to do Unlockable Items for a Character Collection Clicker Game? How to disable buttons? 0 Answers
how to run unity 0 Answers