c# - 在 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# - 预期方法名 --> ; 我的脚本中的预期错误
- python - chainer AttributeError:“NoneType”对象没有属性“ndim”
- node.js - 我们如何在生产服务器上部署 nodemailer nodejs 应用程序
- mysql - 如何解决mysql主从复制错误
- jquery - div高度不同时的过渡速度不一致
- sql - 如何将 sql 查询输出中的值从 1 转换为 yes
- sparql - Sparql:结合删除和描述
- solr - 我如何在 apache solr 中的一组结果上执行/使用中位数?
- vue.js - V-Expansion-Panel 刷新内容
- mysql - 需要获取 MAX(来自 current_ts 列的最新日期)并与我的查询集成