- Home /
Door close problem - Multiple door script
So I have been doing this for a few days now, had problems with a script and been fixing stuff, now I encountered a problem that I dont know how to fix. When I open a door, script that is on that door checks if its closed, if it is closed it sets boolean to true when door open animation is played, same goes if the door is open, it sets boolean to false when door close animation is played. Now, I have this script, and what is not working is closing the door. It wont eve play the Debug.Log and I dont know why is that.
#pragma strict
var doorOpenSound : AudioClip[];
var doorCloseSound : AudioClip[];
var rayLenght = 10;
function Update(){
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if(Input.GetButtonDown("Interaction")){
if(Physics.Raycast(transform.position, fwd, hit, rayLenght)){
if(hit.collider.gameObject.tag == "DoorDefault"){
var checkDoorObject = hit.collider.GetComponent.<object_door_check_open>();
if(checkDoorObject.isOpen == false){
hit.collider.GetComponent(Animation).Play("door_wood_open");
Debug.Log("Opened the door");
GetComponent.<AudioSource>().PlayOneShot(doorOpenSound[Random.Range (0, doorOpenSound.length)]);
GetComponent.<AudioSource>().Play();
}
}
}
}
else if(Input.GetButtonDown("Interaction")){
Debug.Log("Trying to close the door!");
if(Physics.Raycast(transform.position, fwd, hit, rayLenght)){
if(hit.collider.gameObject.tag == "DoorDefault"){
if(checkDoorObject.isOpen == true){
hit.collider.GetComponent(Animation).Play("door_wood_close");
GetComponent.<AudioSource>().PlayOneShot(doorCloseSound[Random.Range (0, doorOpenSound.length)]);
GetComponent.<AudioSource>().Play();
}
}
}
}
}
I have a feeling its a GetButtonDown problem, I dont know why would it open a door and it wont close. I have Interaction input with keycode E *
Answer by siaran · Aug 02, 2015 at 09:42 PM
I have a question for you: Why do you expect your code will ever reach your else-if?
You want to do something more like this: (sorry if I messed up the {}s)
if(Input.GetButtonDown("Interaction")){
if(Physics.Raycast(transform.position, fwd, hit, rayLenght)){
if(hit.collider.gameObject.tag == "DoorDefault"){
var checkDoorObject = hit.collider.GetComponent.<object_door_check_open>();
if(checkDoorObject.isOpen == false){
hit.collider.GetComponent(Animation).Play("door_wood_open");
Debug.Log("Opened the door");
GetComponent.<AudioSource>().PlayOneShot(doorOpenSound[Random.Range (0, doorOpenSound.length)]);
GetComponent.<AudioSource>().Play();
checkDoorObject.isOpen = true;
}else {
hit.collider.GetComponent(Animation).Play("door_wood_close");
GetComponent.<AudioSource>().PlayOneShot(doorCloseSound[Random.Range (0, doorOpenSound.length)]);
GetComponent.<AudioSource>().Play();
checkDoorObject.isOpen = false;
}
}
}
}
The point is, you need to check your if-else on your isOpen boolean, not on if your button is down (it's down in both cases, there is no way to distinguish a button press from, well, the same button press.)
Your answer
Follow this Question
Related Questions
How to limit the transform.Translate distance 1 Answer
Animation in android game not working 2 Answers
Animation doesn't play 1 Answer
The animations not run on the Copied/duplicated enemies (Characters) 0 Answers
how to do a simil RE7 door system 0 Answers