Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Starting and Stopping Animations within a controller from script

Discussion in 'Animation' started by crash664, Jan 5, 2016.

  1. crash664

    crash664

    Joined:
    Nov 22, 2012
    Posts:
    91
    Hi all,

    I have an abject with an Animator attached. I have several states in which the object transitions between repeatedly. It is possible for the user to stop the object, interact with it, and then choose to send it back to where it is.

    I can successfully stop the animatior (state) that it is on with

    Code (CSharp):
    1. GetComponent<Animator>().Stop();
    However, when I wish to send the object back to the starting position, and resume the state cycle from the first in the sequence, I try using:

    Code (CSharp):
    1. GetComponent<Animator>().Play("StateName");
    and it doesn't start the animator again. The layer index and normalizedTime are both default variables so that shouldn't matter.

    Any ideas?

    Thanks!
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    If you're using Unity 5.3, Animator has a new ancestor class, DirectorPlayer. DirectorPlayer.Stop() stops the entire player. I think you need to use DirectorPlayer.Play(playable) before using Animator.Play(state). Instead, what about just setting Animator.speed to 0?
     
  3. crash664

    crash664

    Joined:
    Nov 22, 2012
    Posts:
    91
    These methods seem to be in the Experimental class and I'm not sure of how one would tie the DirectorPlayer to the Animator on my object.

    Also, changing the speed to 0 just make the animator appear to stop. I need to stop it completely so that I can have the object perform other actions.

    Desired functionality is as follows:

    Object is moving by animation --> Stop Animation --> Interaction with object somehow --> when done interacting, return object to starting position (start of animation) and start the animator from the beginning.

    Thanks for you reply! :)
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    I haven't played around much with the changes that Unity 5.3 made to the Animator class. But, according to the documentation, Animator is now a subclass of Experimental.Director.DirectorPlayer. When you call:
    Code (csharp):
    1. GetComponent<Animator>().Stop();
    you're actually calling DirectorPlayer.Stop(). Presumably you need to call DirectorPlayer.Play(playable) before calling Animator.Play("stateName"). I have to confess that, since I haven't really dug into these new 5.3 features yet, I have no idea at the moment what a Playable is. pierrepaul posted an example last summer, but I haven't looked at it yet.
     
    crash664 likes this.
  5. crash664

    crash664

    Joined:
    Nov 22, 2012
    Posts:
    91
    I couldn't figure that out yet, but in the end I figured out that I could simply set the enabled property of the Animator component to false. I saved it's position when disabled, then when I re-enable it, the animator resumes from when it was disabled. So I just send the object back to that location (for cosmetic purposes).

    Thanks for your help. Hopefully they're working on the Mechanim stuff. It has great potential ;D
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    Oh, that might be a change in 5.3. I think in previous versions of Unity when you disabled and re-enabled an Animator all of the parameters would be reset. Anyway, glad it's working now!
     
  7. Appathon

    Appathon

    Joined:
    Jan 25, 2017
    Posts:
    1
    Try
    gameObject.GetComponent<Animator> ().Rebind ();
     
  8. dudzman

    dudzman

    Joined:
    May 19, 2018
    Posts:
    1
    this is the right answer!
     
    alihimdis likes this.
  9. durukanozanalp

    durukanozanalp

    Joined:
    Jan 4, 2019
    Posts:
    12
    i can kiss you!
     
    PKillerio likes this.
  10. PKillerio

    PKillerio

    Joined:
    Aug 9, 2019
    Posts:
    2
    I was just having trouble with my weapon management system with my fps game and have been debugging it for hours THANK YOUUUU:)

    *the problem I had was that my weapon system works on activating and deactivating the weapon object and when I was mid animation and changed the weapons the animator took that frozen animation as a starting point for all animations and was always returning to that broken frozen state when I returned to that weapon once again (activated it) and reloaded f.e.. The solution I implemented was using my knowledge of which weapon I am going to deactivate and then apply your code on it. Thank you again....
     
  11. mdrunk

    mdrunk

    Joined:
    Apr 26, 2014
    Posts:
    11
    still relevant in 2020.
     
  12. nagybotond123

    nagybotond123

    Joined:
    Apr 6, 2020
    Posts:
    1
    Thank you, it helped me a lot :)
     
  13. codemaker2015

    codemaker2015

    Joined:
    Aug 19, 2018
    Posts:
    27
    Stop() method is obsolete in Unity 2019 or greater versions. So use enabled flag and set it to false for disable the animation.

    Code (CSharp):
    1. gameObject.GetComponent<Animator>().enabled = false;
     
    jessicanam likes this.
  14. Henzig

    Henzig

    Joined:
    Aug 18, 2020
    Posts:
    22
    For anybody who finds this as I did and wants to pause all animation within an animatorController.

    I ended up using this instead and it does exactly what I was looking for:
    Code (CSharp):
    1. animationSpeed = GetComponent<Animator>().speed;
    2.  
    3. animationSpeed = 0; // Paused Game
    4. animationSpeed = 1; // UnPaused Game
     
    ALIENPANDA and JDawg666 like this.
  15. jonathandion1994

    jonathandion1994

    Joined:
    Aug 25, 2021
    Posts:
    1
    My hero!
     
  16. UnityZaki

    UnityZaki

    Joined:
    Jan 10, 2018
    Posts:
    18
    Thank you so much you save me
     
  17. Jh2556

    Jh2556

    Joined:
    Sep 29, 2014
    Posts:
    18
    This was what I was looking for . Still works as a solution. Thanks!
     
    marinius313 likes this.
  18. pe86ki

    pe86ki

    Joined:
    Sep 11, 2021
    Posts:
    1
    Thank you, this is works even in 2022
     
    DungDajHjep and marinius313 like this.
  19. chaitanya222

    chaitanya222

    Joined:
    Feb 6, 2023
    Posts:
    1
    This helped a lot. Thanks