- Home /
Question by
Dr.Wookie · Nov 10, 2014 at 07:55 AM ·
animationif-statementssendmessage
If/then statement logic
I want to open and close a door. I have a "doorOpen" animation and a "doorClose" animation. I can get them to play but I'm having a hard time keeping track of if the door is open or closed. The animation handler script is on my door game object. the play animation function is activated in this script by a "sendMessage" in my input handler script.
this is my code to keep track of if the door is closed or not. openAnimation Function is triggered by a sendMessage. Currently this doesn't work because after starting to play the open animation it sets open = true and then starts playing the doorClose animation instead...
thanks for the help
var open = false;
function openAnimation () {
if(open == true){
this.GetComponent.<Animation>().Play("doorClose");
open = false;
}
if(open == false){
this.GetComponent.<Animation>().Play("doorOpen");
open = true;
}
}
Comment
Best Answer
Answer by Linus · Nov 10, 2014 at 07:58 AM
With return you can exit the function asap
var open = false;
function openAnimation () {
if(open == true){
this.GetComponent.<Animation>().Play("doorClose");
open = false;
return; //Exit the function so we dont open the door again
}
if(open == false){
this.GetComponent.<Animation>().Play("doorOpen");
open = true;
}
}
Or you could do:
if(open == true){
} else {
//If not true it must be false
}