通过脚本更改动画速度?

Change the animation speed via script?

public class door : MonoBehaviour
{
    void OnTriggerEnter(Collider obj)
    {
        var thedoor = GameObject.FindWithTag("SF_Door");

        Animation anim;
        anim = thedoor.GetComponent<Animation>();
        anim["open"].speed = 10;

        thedoor.GetComponent<Animation>().Play("open");
    }

    void OnTriggerExit(Collider obj)
    {
        var thedoor = GameObject.FindWithTag("SF_Door");
        thedoor.GetComponent< Animation > ().Play("close");
    }
}

我尝试添加这部分:

Animation anim;
anim = thedoor.GetComponent<Animation>();
anim["open"].speed = 10;

我想让门开得更快,但上面的代码并没有改变速度。 有没有办法通过脚本来完成编辑器中 Animator/Animation windows 中没有 changing/adding 的东西?

首先 - 您需要停止使用动画。而是将 Mecanim 与其 Animator 组件一起使用。这是一个不错的介绍 video.

最简单的方法是在 AnimatorController 中添加一个新的 float 参数 - 我们称之为 speedMultiplier,然后在 AnimatorController select您想要加速的动画并设置如下:

现在不做 anim["open"].speed = ... 而是做:

Animator anim = thedoor.GetComponent<Animator>();
anim.SetFloat("speedMultiplier", 10);

这样动画速度将乘以您为 speedMultiplier 设置的任何值。

添加“.0f”,因为它是一个浮点数。

anim["open"].speed = 10.0f;