- Home /
Need help with my animation script
I'm trying to make a simple script to open and close a door. I'm a noob when it comes to scripting but this is what I have made so far:
function OnMouseDown () {
animation.Play("open");
if (OnMouseDown) {
animation.Play("close");
}
}
But the script does understand that I want to make two different commands. This is what I want to achieve.
When I click on the door the door will open, and STAY open! When I click on it again the door will close and stay that way until I click on it again.
All help is appreciated.
Thanks.
Answer by DaveA · Feb 06, 2012 at 08:42 PM
if (OnMouseDown) doesn't make any sense.
You should set a boolean and test it:
var isOpen = false;
function OnMouseDown () {
if (isOpen)
animation.Play("close");
else
animation.Play("open");
}
Thank you for your reply.
When I use the script with isOpen set to "false" the door repeat the "open" animation every time i click on it, and if I set isOpen to "true" it will repeat the "close" animation every time i click on the door. Can I turn isOpen to "true" when I click the door? If I'm not mistaken your script is automatically trying to decide if the door is open or closed, can I somehow command the door to change status to false/true when I click on it? Like this:
When I click on the door the animation plays and change to isOpen to "true". When I click again the "close" animation plays and change isOpen to "false".
Yes, you should every time you open/close the door change the isOpen boolean to the opposite of what it currently is.
This is the solution:
var isOpen = false;
function On$$anonymous$$ouseDown () { if (isOpen) animation.Play("close"); else animation.Play("open"); isOpen = !isOpen; }
Your answer
Follow this Question
Related Questions
Respawn script and door animation help 0 Answers
Trigger door animation with external trigger 1 Answer
Animation clip does not exist? 1 Answer
Door Movement Animation 3 Answers