- Home /
Animator - Stoping && Playing from Specific frames.
Hello everyone, thank you for stopping by.
Quick quesitons:
1)How could I stop an animation from playing? I want to play a specific animation, lets say "walk".
public void AnimateMovement(){
_animator.Play("walk");
}
But, is there a way to stop this same animation?
public void StopMovement(){
_animator.Stop("walk");
}
I tried this but didn't work, then I tried _animator.animation.Stop but there's no animation component attached, I made the animations using an animator controller.
2)Is it possible to start an animation from any other frame other than the first? For example
public void StartMovement(){
_animator.Play("walk", _animator("walk")[2]);
}
**Start walk animation from frame 2?
Thank you for your time and knowledge.
Answer by Noob_Vulcan · Sep 18, 2015 at 06:45 AM
Well if you want to do that in Animator then use this
Lets say you want to play the animation from the mid frame .This is how u will do it
getComponent().Play(“Animation_Name”,0,0.5f);
or
getComponent().Play(“Animation_Name”, 0 , (1/total_frames)*frame_number);
For more you can refer here http://www.unityrealm.com/play-animation-from-frame-unity-5-animator/
Answer by maximilianahead · Mar 27, 2015 at 01:24 AM
1)How could I stop an animation from playing?
One way, stop the animator speed;
_animator.speed = 0f;
Second, create the new state named by Pause.
For example, the transition conditions to Pause state:
_animator.SetTrigger("Pause");
you save to state when pause, check off the "Write Defaults" parameter of Pause state.
2)Is it possible to start an animation from any other frame other than the first?
you can play specifying the normalized time.
for example, If you start from the middle:
_animator.Play("walk", -1, 0.5f);
Answer by wesleywh · Dec 15, 2014 at 05:49 PM
First off I'm not an expert on this but the following could be your problem. So your using the "Animator" component not the "Animation" component? That could be your problem. The Animation component you can use your code that you have above and it should work. However, if you use that for the "Animator" component that won't work unfortunately. You will have to edit the variables that control your walk. For example if your walking your speed > 0.1f so set the speed variable to zero or above to walk or stop. So for example: JavaScript:
var anim : Animator;
function SetSpeed(){
anim.SetFloat("speed", 1.3f);//Set the speed variable to 1.3f and the animator takes care of the rest.
}
C#:
public Animator anim;
void SetSpeed(){
anim.SetFloat("speed", 1.3f);//Set the speed variable to 1.3f and the animator takes care of the rest.
}
Hope this helps!
Thank you for your time. I Forgot to mention that I was using "Animator Controller". Now, i tried to do this with "Animation" ins$$anonymous$$d but when I try to do Play("walk"); nothing is happening, even though I created the animation on the 'Animation' tab and just did "create new clip" and attached it to the component...
For the "Animation" component the animations will only play on that character if those animations were built specifically for that character. If you are trying to use a universal animation, like walk on both character A and character B, it won't play.
However, since you are using an "Animator" component that shouldn't be a problem and you should be able to play that animation on any character. Then next thing to check is to make sure that the variable that is deciding what state to play is being initialized correctly. Like the example above you can't just call "Walk" you have to call the variable that will tell the character to go to the Walk state.
Hopefully all of that makes sense.
Answer by Feelnside · Jan 04, 2018 at 05:11 PM
Thanks to everyone! Probably some one is looking for a way to find out a normalized time of the running animation.
In my case I'm using the following line to get the current normalized time of the running animation:
float_var = animator.GetCurrentAnimatorStateInfo(0).normalizedTime;
And after that I can replay the animation from the particular frame as it was mentioned above:
animator.Play("anim_name",0,float_var);
Answer by samra2494 · Mar 07, 2019 at 05:55 AM
you can stop animation on any frame you want.. for example you want to stop animation at the position of very first frame. just use these line of code
//stop animation .. on first frame
gameObject.GetComponent<Animator>().Rebind();
please try this above line if it not work for you . try the blow line of code OR //use this line to stop on very first frame which is 0.0f and the layer is 1 gameObject.GetComponent().PlayInFixedTime("ClipName",1,0.0f);
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Multiple Animation Controllers 1 Answer
How to make not smooth animation? 0 Answers
Best Way - Animation - Multiple GameObjects. 0 Answers
Object not moving after changing animator parameter 1 Answer