4
\$\begingroup\$

I'm new to Unity and after doing some tutorials and reading documentation from Unity, I've started a test project. The game is a 2.5D platformer and I wrote the PlayerController; it looks like this:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

    public float turnSmoothing = 15f;
    public float speedDampTime = 0.1f;

    private Animator anim;
    private Rigidbody rb;
    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator>();
        anim.SetLayerWeight(1, 1f);

        rb = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update () {

    }

    void FixedUpdate()
    {
        float h = Input.GetAxis("Horizontal");
        MovementManagement(h);
    }

    void MovementManagement (float horizontal)
    {
        if (horizontal != 0.0f)
        {
            anim.SetBool("Move", true);
            anim.SetFloat("Speed", 5.5f, speedDampTime, Time.deltaTime);
            Rotating(horizontal);

        }
        else
        {
            anim.SetFloat("Speed", 0.0f);
            anim.SetBool("Move", false);
        }
    }

    void Rotating (float horizontal)
    {
        Vector3 targetDirection = new Vector3(horizontal, 0.0f, 0.0f);
        Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
        Quaternion newRotation = Quaternion.Lerp(rb.rotation, targetRotation, turnSmoothing * Time.deltaTime);
        rb.MoveRotation(newRotation);
    }
}

My AnimatorController has a BlendTree called Locomotion and looks like this:

enter image description here

When I run the game and use the keyboard or the controller the player starts walking, but it takes a long time (I can see the Speed parameter from the blend Tree that has to reach "5" to start walking)

I think I'm not understanding something but I don't know what it is.

\$\endgroup\$
3
\$\begingroup\$

When you trigger the transit from one animation state to another with a parameter condition, that condition won't be checked until the exit time of the transition is reached in the current animation cycle. So when you have a very long idle animation with a long exit time, it can take a while until the conditions for changing to the walk animation is checked and the animator blends to HumanoidWalk. For example, when your idle animation is 2 seconds and your exit time is 0.8, it can take up to 1.6 seconds until the transition happens.

To fix this issue, reduce the exit time or remove it altogether by unchecking "Has Exit Time" in the transition.

Another problem might be a too long "transition duration".

As an alternative way to switch to a different animation state immediately, you can also use anim.Play which will skip the transition altogether.

\$\endgroup\$
  • 1
    \$\begingroup\$ Thanks, It was the problem, the idle animation was to long, so I unchecked the exit time and the new animation overrides the idle. Thanks. \$\endgroup\$ – user1423168 Sep 9 '15 at 15:51
  • \$\begingroup\$ While your explanation of the problem is correct, your fix is not. He found the correct fix (uncheck the "exit time" property). \$\endgroup\$ – jhocking Sep 9 '15 at 16:30
  • 1
    \$\begingroup\$ @jhocking updated answer \$\endgroup\$ – Philipp Sep 9 '15 at 16:47

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.