- Home /
isOpen queues next door to run close animation even if its closed
I have done a script that is not working how i wanted it to. As the title says, it runs close animation even if the door is closed. I tried using String arrays to get gameobject name to compare if its the same name of the object and it doesnt work I dont know why.
Code sample:
#pragma strict
private var isOpen : boolean = false;
var doorOpenSound : AudioClip[];
var doorCloseSound : AudioClip[];
var rayLenght = 10;
public var isSameDoor : boolean = false;
public var doorName : String[];
function Update(){
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if(Input.GetButtonDown("Interaction") && isOpen == false){
if(Physics.Raycast(transform.position, fwd, hit, rayLenght)){
if(hit.collider.gameObject.tag == "DoorDefault"){
doorName[0] = hit.collider.gameObject.name;
if(!(doorName[1] == doorName[0])){
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();
isOpen = true;
}
}
}
}else if(Input.GetButtonDown("Interaction") && isOpen == true){
if(Physics.Raycast(transform.position, fwd, hit, rayLenght)){
if(hit.collider.gameObject.tag == "DoorDefault"){
doorName[0] = hit.collider.gameObject.name;
if(!(doorName[1] != doorName[0])){
hit.collider.GetComponent(Animation).Play("door_wood_close");
Debug.Log("Closed the door");
GetComponent.<AudioSource>().PlayOneShot(doorCloseSound[Random.Range (0, doorCloseSound.length)]);
GetComponent.<AudioSource>().Play();
isOpen = false;
}
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
open and close door 0 Answers
Door will not open with the press of a button 1 Answer
Door opens, plays sound, but can't close it. 2 Answers
Open & Close a simple door animation with sound and door states 1 Answer
Animating clip with transitions 2 Answers