将动画与方向Unity对齐

时间:2017-07-09 20:38:08

标签: animation unity3d direction

所以问题是当我移动角色(他不是人形)时,他的步行动画与他的动作方向不对齐,例如,如果我按下W然后他向前移动但他的步行动画向左转90度,我不知道问题可能在哪里。这是我的代码:

public class PlayerControler : MonoBehaviour
{

    public float MovSpeed = 3;

    public float SmoothTime = 0.1f;
    float TurnSmoothVelocity;

    public float SpeedSmoothTime = 0.1f;
    float SmoothVelocity;
    float CurrentSpeed;

    Animator animator;

    void Start()
    {
        animator = GetComponent<Animator>();
    }

    void Update()
    {

        Vector2 Input = new Vector2(UnityEngine.Input.GetAxisRaw("Horizontal"), UnityEngine.Input.GetAxisRaw("Vertical"));
        Vector2 InputDirection = Input.normalized;

        if (InputDirection != Vector2.zero)
        {
            float TargetRotation = Mathf.Atan2(InputDirection.x, InputDirection.y) * Mathf.Rad2Deg;
            transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y , TargetRotation, ref TurnSmoothVelocity, SmoothTime);
        }

        float targetSpeed = MovSpeed * InputDirection.magnitude;
        CurrentSpeed = Mathf.SmoothDamp(CurrentSpeed, targetSpeed, ref SmoothVelocity, SmoothTime);

        transform.Translate(transform.forward * CurrentSpeed * Time.deltaTime, Space.World);

        float animationSpeedPercent = .5f * InputDirection.magnitude;
        animator.SetFloat("SpeedPercent", animationSpeedPercent, SpeedSmoothTime, Time.deltaTime);

    }
}

1 个答案:

答案 0 :(得分:1)

动画对象应该是已分配此脚本的Controller的子项。

只需制作一个EmptyGameObject,添加此脚本并将该动画角色置于该对象下。

如果它仍然无法正常工作,动画可能会出现问题。脚本看起来对我来说。