0
\$\begingroup\$

The code is simple. If the player presses the right arrow key, one animation clip plays. If the left arrow key, another clip plays, etc. However, the clip doesnt play fully unless the input key is held continuously. The second you release it, the second the clip stops playing. I think it's something to do with update() and the input not being remembered over next frame.

The code is simple:

using UnityEngine;
using System.Collections;

public class animatorcontroller_Logic : MonoBehaviour 
{

    Animator anim;

    enum MovementState
    {
        Idle = 0,
        run = 1,
        walk = 2,
    }

    MovementState currentMovementState;

    void Start () 
    {
        anim = gameObject.GetComponent<Animator> ();
    }

    void Update () 
    {

        if(Input.GetButton("right")) 
        {
            currentMovementState = MovementState.run;

        }

        else if(Input.GetButton("left")) 
        {
            currentMovementState = MovementState.walk;

        }

        else
        {
            currentMovementState = MovementState.Idle;    //set movement state to idle
        }


        anim.SetInteger("MovementState", (int)currentMovementState);    

    }

}
\$\endgroup\$
1
\$\begingroup\$

On the Update() call immediately following releasing the key, your else clause sets currentMovementState = MovementState.Idle. This stops the walk animation, I presume.

\$\endgroup\$
  • \$\begingroup\$ How can I fix this? I am still an amateur coder. \$\endgroup\$ – gamere May 28 '15 at 20:41
  • 1
    \$\begingroup\$ Remove the else clause, and it will stop doing this. How you deal with these issues depends on how your game is set up, and is beyond the scope of an answer to your question. \$\endgroup\$ – Almo May 28 '15 at 20:42
  • \$\begingroup\$ thing is if I remove else, it wont go back to idle as the parameter will remain set at 1 and it will keep animating the current clip forever. \$\endgroup\$ – gamere May 28 '15 at 21:34
  • \$\begingroup\$ have the walk cycle animation dispatch an event in the loop when it finishes. check for idlenss then. \$\endgroup\$ – Plastic Sturgeon May 29 '15 at 0:45
0
\$\begingroup\$

I figured it out. I just need to set the transition condition to Exit Time = 0.95 in the state machine instead. This ensures the animation plays fully before going back to idle.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.