- Home /
Stop animation after playing once
I'm using RaycastHit to script a door opening animation when my character comes within a few feet of the door. How do I get the animation (of door sliding out of the way) to play only one time.
Right now, once I'm within the specified distance, the door animation keeps playing over and over (so keeps opening and closing until I move back out of distance).
Animation simply has door keyed to normal position at 0 frames, and then position.y made negative so door slides into ground at 30 frames. And here's the script I'm using:
var rayCastLength = 10;
function Update ()
{ var hit:RaycastHit;
//check if we're colliding if(Physics.Raycast(transform.position, transform.forward, hit, rayCastLength)) { //with a door - need to add the 'collider' in this code since using Raycast instead of just collision if(hit.collider.gameObject.tag == "door") { //open the door hit.collider.gameObject.animation.Play("doorOpen"); }
}
}
Answer by Paulius-Liekis · Dec 20, 2010 at 04:10 PM
Setting ClampForever wrapMode on your door animation should do the trick.
Thanks. That fixed it. So did play once and even default. I must have had it set on Loop inadvertently.
On a related note, if I want the animation to reverse if I move out of range (so door only stays open when I walk near it), is there a simple way to do that? Or a way to have the door stay open for 5 seconds and then close?
Thanks
O$$anonymous$$ - wasn't just my imagination - there seems to be a glitch. If I set the doorOpen animation on Once, it repeats itself many times over a short time period - so the door races up and down about 5 to fifteen times - then after going through all those cycles, the door remains fixed in the open position.
(With clampForever this behavior didn't happen. And with Once, it seems inconsistent whether or not it Loops several times or works properly from the outset).
Is this expected behavior or some bug in the system (or in my noobie code more likely)
The Once behavior might be by design - once "Once" is done it resets time to 0, so if you call Play again, it will play again. ClampForever will make animation get stuck in last frame. You can reverse playbakc by setting animation speed to negative, although I would suspect that there might be some bugs related to that in Unity 3.1 (I fixed some of those in 3.2).
Ok. Thanks for the clarification. Now that you've described how it works, I think I should be able to add some simple javascript to get the behavior I desire.
Ok. Tried adding some code in javascript to stop the door from opening and closing repeatedly but didn't have any luck. Basically assigned a variable that would be changed after the door first opened, and tried to use that variable to stop the animation from being played again - but not working for some reason. I posted the relevant code snippet below in hopes someone could tell me where I messed up. Don't know if this is due to mistake on my part or if Unity scripting has to follow certain rules I'm not aware of.
Thanks - code in next post as running out of room:
Your answer
![](https://koobas.hobune.stream/wayback/20220613091800im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Animation playing over and over 0 Answers
Unity Animator cant play the same animation twice. 2 Answers
Character won't run straight 1 Answer