首页 > 解决方案 > 在 Unity c# 中,我无法使用 animation.SetInteger 让动画过渡

问题描述

我正在尝试使用 Animator Controller 组件从空闲动画过渡到行走动画。我似乎无法让 animator.SetInteger 为我工作。我已经尝试过使用和不使用 else 语句。如果我把它放在启动函数中它会起作用,但我需要将它绑定到步行输入。移动工作正常,通过公共浮动我可以看到verticalInput在我按下键时发生变化。我还尝试将 Animator 声明为公共或私有。

没有编译器错误,但是当我点击播放时 Unity 会抛出此错误:NullReferenceException: Object reference not set to an instance of an object.

运动仍然可以正常播放,只是没有动画,除非我通过组件手动更改“条件”参数。

我也试过Debug.Log(animator.GetInteger("Condition"))了,但我想那样不行。

这是我的脚本:

public class PlayerController : MonoBehaviour
{
    public float speed = 8;
    public float rotationalSpeed = 45;

    public float verticalInput;
    private float rotationalInput;

    private Animator animator;


    void Start()
    {
        Animator animator = GetComponent<Animator>();
        //animator.SetInteger("Condition", 1);
    }

    void Update()
    {
        verticalInput = Input.GetAxis("Vertical");
        rotationalInput = Input.GetAxis("Horizontal");

        transform.Translate(Vector3.forward * verticalInput * speed * Time.deltaTime);
        transform.Rotate(Vector3.up * rotationalInput * rotationalSpeed * Time.deltaTime);

        
        if (verticalInput != 0)
        {
            animator.SetInteger("Condition", 1);
        }
        else
        {
            animator.SetInteger("Condition", 0);
        }
        Debug.Log(animator.GetInteger("Condition"));
    }
}

我在这里做错了什么有什么想法吗?

标签: c#unity3danimation

解决方案


推荐阅读