Search Unity

Stopping an animation on a specific frame?

Discussion in '2D' started by MasterElement, Jun 9, 2019.

  1. MasterElement

    MasterElement

    Joined:
    Jun 13, 2016
    Posts:
    140
    So I'm making a 2D game where you walk around like in pokemon for GB or Lengend of zelda LTTP. In my animations for walking I have on in the middle of the cycle where the character is standing still. I want to code it so when the directional key is released the current walking animation stops on that particular frame. I have done searches on this but people just gave a function to use with out explaining how to, and the documentation on that function didn't go into it either so I'm stuck.
    Any ideas?
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Give your animation clip a multiplier value that is controlled through script via an animation float variable (see Animator.SetFloat).

    Setting this value to 1 will make the animation play at normal speed, and 0 will make it pause. Just switch between these two values when the player is moving and not moving.

    You can use this value in other ways as well:
    • A value between 0 and 1 will make the animation play slower.
    • A value greater than 1 will make the animation play faster.
    • A value less than 0 will reverse the animation.
    • A value of -1 will make the animation play reversed at regular speed.
    • Etc...
     
    Last edited: Jun 9, 2019
    JevinLevin_, Rechronicle and Crisby like this.
  3. MasterElement

    MasterElement

    Joined:
    Jun 13, 2016
    Posts:
    140
    Thanks but I don't see how that makes the animation freeze on a particular frame though?
    I need it to stop on the one where the character's arms and legs are at their sides like they are standing.
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Ah my mistake. In this case, you can just make this one frame it's own animation clip and transition to it when the character stops moving.

    There's possibly a better way of doing this, but I haven't experimented with this circumstance.
     
  5. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
  6. MasterElement

    MasterElement

    Joined:
    Jun 13, 2016
    Posts:
    140
    Alright thanks both of you
     
  7. MasterElement

    MasterElement

    Joined:
    Jun 13, 2016
    Posts:
    140
    Okay so I looked into Vakabaka's answer and To me it seems like the animation events in Unreal engine, in that you can make it so when a character's walking animation reaching a point where a puff of dust appears. However the Documentation doesn't make it clear enough to me that you can revert the animation to a specific frame when a variable activates.

    @Vryken2 I tried your idea, but you can't make a single sprite into an animation, so how would I do what you suggested?
     
  8. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    Code (CSharp):
    1.     Animator animator;
    2.  
    3.     private void Start()
    4.     {
    5.         animator = GetComponent<Animator>();
    6.     }
    7.     public void StopAnimation ()
    8.     {
    9.         animator.speed = 0;
    10.     }
    then on the needed frame add an event and choose the StopAnimation function on it. In this public funktion you can do something with your animation.

    sorry i misunderstood you, if you will stop animation by input keys, then get the animator component and stop animation with the c#. This will be allmost the same. Just call the animator in the c# script at needed time and do what is needed.

    Code (CSharp):
    1. Animator animator;
    2.  
    3.     private void Start()
    4.     {
    5.         animator = GetComponent<Animator>();
    6.        
    7.     }
    8.     public void Update ()
    9.     {
    10.         Input.GetKeyUp(KeyCode.RightArrow)
    11.         {
    12.             animator.speed = 0;
    13.         }
    14.     }
     
    Last edited: Jun 11, 2019
    HayalOrtak and krupps like this.
  9. MasterElement

    MasterElement

    Joined:
    Jun 13, 2016
    Posts:
    140
    I still don't see how that helps me stop the animation on the frame where the player is just standing there with their feet together
     
  10. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    • Select your character and have the Animator window open.
    • From the Animation window, click the option to create a new animation.
    • Make this new animation one frame long and just display the one sprite you want.
    • Add transitions between this animation and your walking animation, and make the transitions occur when the character starts/stops moving.
     
  11. bart_the_13th

    bart_the_13th

    Joined:
    Jan 16, 2012
    Posts:
    498
    make 2 animationclip in animation windows. walk and stand..
    fill walk animation with walking sprite set and stand clip with any standing sprite..
    call animator.play("walk") when you start walking and call animator.play("stand") when you stop walking...
    also you can use animatiom event to generate smoke on certain frame on walking
     
  12. Crisby

    Crisby

    Joined:
    Oct 1, 2019
    Posts:
    5

    I spent a week trying to figure this out last year to no avail, you are an absolute life saver.
     
    annasmags likes this.
  13. amzg-maki

    amzg-maki

    Joined:
    Mar 12, 2013
    Posts:
    12
    I know this is old but can you not set the animation speed to 0 then
    Code (CSharp):
    1. Animator myAnim;
    2. myAnim.Play("animationName", 0, specificFrame/(float)totalAnimationFrames);
    With this approach you will first need to find out the total frames count of your animation clip though. Didn't test this either, but that's what I would do if I want to stop an animation at a specific frame.
     
    Gigabitten_Gaming and Powzone like this.