- Home /
How to stop animation from playing in c#.
How to stop animation from playing in the animator in c# script. Without using myAnimator.SetBool("Animation", true); - because I already know how to use this.
myAnimator.speed = 0; to stop animation, when you want to freeze of make someone become stone myAnimator.speed = 1; to return
Answer by tmalhassan · Sep 27, 2017 at 01:53 PM
Hello @DiamondMC102.
To stop an animator from playing the animation, simply, disable the animator component using this code:
myAnimation.gameObject.GetComponent<Animator>().enabled = false;
I have also found that disabling the gameObject itself will also stop the animation from playing until it is enabled again.
I hope you find this information helpful. And don't hesitate to reply back if you have any further enquirers or need more assistance.
All the best.
This doesn't seem to work as intended for me. Here's the code I used:
using UnityEngine;
public class GunAnimation : $$anonymous$$onoBehaviour
{
Animator m_Animator;
void Start()
{
//Fetch the Animator from your GameObject
m_Animator = GetComponent<Animator>();
}
private void Update()
{
if (OVRInput.GetDown(OVRInput.Button.SecondaryIndexTrigger) || Input.Get$$anonymous$$ouseButtonDown(0)) // Secondary means Right Hand for Oculus Touch
{
m_Animator.GetComponent<Animator>().enabled = false;
m_Animator.GetComponent<Animator>().enabled = true;
m_Animator.Play("FullGunFireSequence");
}
}
}
But when I press the fire button rapidly it's not stopping and then starting the animation from scratch again on each button press. I basically expect it to be interrupting the animation mid flow with this solution, so the gun visibly twitches unless I stop firing so quickly and let the actual full animation sequence finish, but it seems to play out the full animation once before it plays it again as per normal, even if I press the button so quickly it should interrupt the current animation constantly.
Any ideas what I'm doing wrong?
Hello @impurekind, i am not sure this is what you meant, but, i belive what you're trying to do is "restart" the animation? if so, disabling the entire gameobject and re-enabling it would help you do that. With this code:
m_Animator.gameObject.SetActive(false);
m_Animator.gameObject.SetActive(true);
You might also notice that i removed the .GetComponent<>()
part from you Update()
function. I did this because you already get the component that you want in Start()
, that and the fact that we're disabling the object itself, not the component only.
Hope this helps.
O$$anonymous$$, I just found the solution after quite some checking (this has now taking me over a day to finally get resolved). It was to use the same Play command but with some stuff inside the brackets (I forget what you call those things, conditions maybe):
m_Animator.Play("FullGunFireSequence", -1, 0f);
Now I don't exactly understand what the "layer" part of that is, I just left it at the same value in the example, but it seems to have worked.
Do I insert the name of my animation in my animation?
Answer by samra2494 · Mar 07, 2019 at 05:50 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().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 solution works but your answer code formatting is terrible.
I know it's old, but worked like a charm! Thanks! A little tip for those will follow this way, check if the animation is the one you want to stop before Rebind() it.
Answer by Elestin · Jun 30, 2020 at 01:12 PM
You can set speed multiplier to 0. Create a new parameter in animator window. Then click on the animation in animator window. Click on the parameter button next to Multiplier(below Speed). Then change it in the code:
m_Animator.SetFloat("speedMultiplier", 0f);
Answer by codemaker2015 · Oct 27, 2020 at 07:41 AM
//For playing the animation
gameObject.GetComponent<Animator>().Play();
//or
gameObject.GetComponent<Animator>().enable = true;
//For stop the animation
gameObject.GetComponent<Animator>().enabled = false;
DO NOT use GetComponent every time you need to access it. Create a variable that stores the animator and either assign it on Start() once with GetComponent, or assign it via the inspector.
Answer by Stonemass · Jan 23, 2021 at 02:55 PM
super normal, to stop an animation we must to desactivate the component.
Stop, play or repeat looks so weird to use with animations. ok