Search Unity

Unable to get animation state to loop

Discussion in 'Animation' started by manrock007, Dec 2, 2013.

  1. manrock007

    manrock007

    Joined:
    Mar 9, 2013
    Posts:
    26
    Hi,

    I'm currently in the process of learning the mecanim system and for some reason i can't seem to loop the locomotion blend tree. The animation plays once, goes back to idle and then comes back to locomotion as driven by the script. So it keeps transitioning between the states idle and locomotion, whereas according to the expected behavior it should not leave the locomotion state until the speed variable becomes 0 again.

    Here's a video to show you what's happening.



    I have a basic animator controller setup and i'm providing the speed and direction inputs to the animator controller using a simple script.

    $animator_controller.png

    I have also ensured that the imported animation has loop pose checked and loop match indicators set to green. Although i cant get the root transform XZ to green.(A problem with the animation ??)

    $animation_inspector.png


    Finally, here's the script i use to drive the animations-

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class mecanim_driver_cs : MonoBehaviour {
    5.     private Animator  anim;
    6.     // Use this for initialization
    7.     void Start () {
    8.         anim = GetComponent<Animator>();
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.    
    14.         float verInput = Input.GetAxis("Vertical");
    15.         float horiInput = Input.GetAxis("Horizontal");
    16.         anim.SetFloat("speed",Mathf.Clamp(verInput,0,1), 0.2f, Time.deltaTime);
    17.         //anim.SetFloat("direction",Mathf.Clamp(horiInput,0,1), 0.2f, Time.deltaTime);
    18.     }
    19.    
    20.     void FixedUpdate(){
    21.        
    22.     }
    23. }
    I could't find anything related to this issue when googling for "unable to loop animations in mecanim". Apologies if this has been discussed already.

    Regards,
    Manish N
     
  2. manrock007

    manrock007

    Joined:
    Mar 9, 2013
    Posts:
    26
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You haven't shown us the most important bit, which is the transition from forward_locomotion_fast to idle. What's the condition on that transition?

    Also note that you don't need to write (or show us) script to test this stuff... arrange your panels so you can see the Animator at the same time as your game, and then just manually tweak the parameters in the lower-left corner of the Animator panel while the game runs. The state machine will do its thing accordingly.
     
  4. manrock007

    manrock007

    Joined:
    Mar 9, 2013
    Posts:
    26
    The transition between the 2 states depends on the variable speed

    Idle to locomotion : speed greater than 0.1
    Locomotion to idle : speed less than 0.1

    I have already tried the method you described, and in the animator controller panel I can see that the locomotion state plays, then transitions back to idle for a split second and then jumps back into locomotion.
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Can you post a screen shot of the Locomotion to Idle transition? I think that the difficulty must be there.

    Also (just to be sure), what happens if you delete the Locomotion to Idle transition altogether?
     
  6. manrock007

    manrock007

    Joined:
    Mar 9, 2013
    Posts:
    26
    I will post the screenshot once i get home, but i don't think that's the problem. I say this cause I've already tried deleting the locomotion to idle transition and it makes no difference whatsoever.
    The real problem is that the state machine transitions to the idle state for a split second after locomotion is played completely. I cannot get the locomotion state to repeat. Running the animation in the preview window shows that my locomotion animation loops properly, it just doesn't loop in the actual gameplay.
     
  7. manrock007

    manrock007

    Joined:
    Mar 9, 2013
    Posts:
    26
    Here are the screenshots for transitions between idle and locomotion

    Idle -> Locomotion

    $idle_to_locomotion.png

    Locomotion -> Idle

    $locomotion_to_idle.png
     
  8. abeaulne

    abeaulne

    Joined:
    Oct 2, 2013
    Posts:
    2
    I don't know what you max speed is, but I see that the speed values for your transitions are inconsistent. I would try setting "speed less than 0.1" for your transition from loco to idle.
     
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Also, could it be that the return to standing is actually in the run animation? Perhaps the run clip was taken out of a larger animation sequence, and not properly trimmed?
     
  10. manrock007

    manrock007

    Joined:
    Mar 9, 2013
    Posts:
    26
    I was able to resolve the issue 5 minutes back. I had a transition setup from "Any state" -> Idle and that's what was causing the state change. Removing that gets my state machine to work flawlessly.
     
    SirhotBay likes this.
  11. nichallan

    nichallan

    Joined:
    Mar 20, 2023
    Posts:
    1
    Thank you for posting your fix when you found it, I've been trying to figure this out for an hour.