脚本可以挂到玩家身上
需要给玩家添加动画组件Animator
在动画组件里放入需要播放的动画。
在公开的动画变量中放入动画组件。
NavMeshAgent agent; // 导航组件
public Animator anim; // 获取动画
float motionSmoothTime = 0.1f;
// Start is called before the first frame update
void Start()
{
agent = GetComponent<NavMeshAgent>();
// 获取导航组件
}
void Update()
{
// 路程除以时间等于速度
float speed = agent.velocity.magnitude / agent.speed;
// 设置动画Speed浮点数的值以及跟新的频率随着时间增加
anim.SetFloat("Speed",speed,motionSmoothTime,Time.deltaTime);
}