Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by DiamondMC102 · Sep 27, 2017 at 12:40 PM · c#animationscripting problemanimator

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Talmagett · Dec 23, 2020 at 12:10 PM 0
Share

myAnimator.speed = 0; to stop animation, when you want to freeze of make someone become stone myAnimator.speed = 1; to return

7 Replies

· Add your reply
  • Sort: 
avatar image
17
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DiamondMC102 · Sep 28, 2017 at 03:21 AM 0
Share

Thanx. $$anonymous$$ate

avatar image amirl_unity · Apr 29, 2018 at 01:16 AM 0
Share

thanks, first helpful answer i have found :)

avatar image impurekind · Aug 23, 2018 at 12:20 PM 1
Share

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?

avatar image tmalhassan impurekind · Aug 23, 2018 at 12:40 PM 0
Share

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.

avatar image impurekind tmalhassan · Aug 23, 2018 at 05:17 PM 1
Share

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.

Show more comments
avatar image unity_wIfM9PlhP0UL9A · Mar 12, 2020 at 03:43 PM 0
Share

Do I insert the name of my animation in my animation?

avatar image
2

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);

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Develax · Feb 23, 2020 at 06:35 PM 0
Share

Your solution works but your answer code formatting is terrible.

avatar image unity_5YXzT1bRdzbB2g · Dec 28, 2021 at 11:19 PM 0
Share

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.

avatar image
0

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);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BasicallyGames · Jan 28 at 09:10 PM 0
Share

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.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

478 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Have some minor problems. Cant tell if its a script issue or a animator issue. 1 Answer

Is there a new retargetting system for the animation in 5.5? 1 Answer

How can i use the Animator in script to find when animation clip finished playing and then stopping the animation ? 1 Answer

Why the speed parameter in Animator change the animation speed only for door open but when the door is closing the speed still very fast ? 1 Answer

Help with animator controllers 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges