如何获取速度
时间: 2023-10-10 21:14:02 浏览: 33
您可以使用Animator.GetFloat函数来获取速度。在Animator Controller中,通常会有一个名为“Speed”的浮点型参数,该参数会根据动画播放的速度进行修改。您可以使用Animator.GetFloat("Speed")来获取该参数的值,进而获取动画的播放速度。以下是一个示例代码:
```csharp
using UnityEngine;
public class AnimatorMovement : MonoBehaviour
{
private Animator anim; // 声明一个 Animator 变量
private void Start()
{
anim = GetComponent<Animator>(); // 获取该游戏对象的 Animator 组件
}
private void OnAnimatorMove()
{
if (anim)
{
// 获取 Animator 组件中的 root motion
Vector3 deltaMovement = anim.deltaPosition;
// 获取动画的播放速度
float speed = anim.GetFloat("Speed");
// 根据时间缩放 root motion,获得每秒移动距离
deltaMovement *= Time.deltaTime * speed;
// 将移动距离应用到游戏对象的 transform 上
transform.position += deltaMovement;
}
}
}
```
在该示例代码中,我们通过调用Animator.GetFloat("Speed")来获取动画的播放速度,然后将其应用到移动距离上以控制游戏对象的速度。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)