- Home /
How to have an animation loop only twice
Hey guys, Im new to animation, but ive attached a 2d animation to a 3d object in the inspector. so far I've made it so the animation plays once when the child 2d animation is set to active. I would like it to play 2 or more times. How do I set an animation to play only a certain number of times and then disappear after it is done? You do not need to code for me, just point me in the right direction. Thanks,
Images for reference!!
The curvey slash thing is what i want to loop twice (shown below) http://imgur.com/MZgqQDA
here it is attached to the gameobject as a child http://imgur.com/BuR8Anf
Answer by kacyesp · Sep 02, 2014 at 05:26 AM
I think this is what you're looking for: http://docs.unity3d.com/ScriptReference/Animation.Play.html
Just use the play() function in a script to play the animation however many times you want.
@kacyesp I tried using animation.play and ran into a bunch of problems. first of all I have this animation on a child object, which is fine as I call the object like this :
spinAnim = transform.Find("spinslash2d").gameObject;
spinAnim.animation.Play ("Slash");
I set the inspector to debug mode and changed the animations to "1" if you understand what I mean. THe problem im having is that the animations dont appear at all and there is no errors... also, the animations work fine when i use an animation controller... If there is a way to do what I want using an animation controller that would be great because then I could take that route. Do you know how? If not, do you know what my problem would be or some things I could check? Thanks,
@camtronius Yeah you can use an animator. In the animator window, create a parameter of type int, and let's just call it Counter. Add a transition from the state you want to play twice to some other state. Set that transition's condition to Counter greater than 1. In your actual script, you'll create a Counter variable there too just so you can use something like animator.SetInteger( "Counter", Counter ). Here's the link to the docs for that function: http://docs.unity3d.com/ScriptReference/Animator.SetInteger.html
Then create a function, say IncrementAnimationCounter(), in the script whose sole job is use that SetInt function and increment the Counter.
Finally, add an event to the last keyframe of your animation, where that event will call IncrementAnimationCounter(). If you've never added an event before, see this link for how to do it: http://docs.unity3d.com/$$anonymous$$anual/animeditor-AnimationEvents.html
You can play around with the Counter value in case I'm off by 1, but this is how I would do it. Don't forget to reset the Counter.
@kacyesp took me a long time to finally get it but i got it. Wow. 1 more question though. How do I get it from always having the first frame on my object? In otherwords, I dont want to see any of the animation before it is played. Thanks for your help!
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Animator State Freezing 0 Answers
How to optimize 2D Animations created with 2D Animation packege? (v3.1.1) 0 Answers
Animation Lag when Changing Movement Direction 2D (4.3) 2 Answers
Animation stops rotation 0 Answers