Friday, December 20, 2019

unity - Why does my player walk animation take so long to begin?


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();
anim.SetLayerWeight(1, 1f);

rb = GetComponent();
}

// 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.



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...