c# - Unity动画播放两次

标签 c# unity3d

我创建了一个名为 AnimationController 的类,但我遇到了问题。当我按下 space 时,它会触发 Animator 中的 Jump 动画,但它会播放两次。我不明白为什么要这样做。有什么更好的方法来处理这个问题?

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Animator))]
public class AnimationController : MonoBehaviour {

    Animator animator;
    AnimatorStateInfo baseLayer;

    void Start() {
        animator = GetComponent<Animator>();
        baseLayer = animator.GetCurrentAnimatorStateInfo(0);
    }

    void Update () {
        if(Input.GetButton("Jump") && !baseLayer.IsName("Jump")) {
            animator.SetTrigger("Jump");
        }

    }
}

最佳答案

我是 Unity 的新手,但我怀疑这是因为您使用的是 GetButton 而不是 GetButtonDownGetButtonUp

从文档中,GetButton:

Returns true while the virtual button identified by buttonName is held down.

因此,如果您按住空格键两帧(即使您按下了一次,物理 Action 所需的时间也比一帧长),那么它会触发两次。如果您按住它的时间超过此时间,它会继续发射。

如果您改用 GetButtonDownGetButtonUp,它应该只触发一次,即在注册按下的确切帧。

Returns true during the frame the user pressed down the virtual button identified by buttonName.

关于c# - Unity动画播放两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32633642/

相关文章:

c# - 统一健康和装甲

c# - Unity重复生成,未使用的生成位置,问题

c# - Unity 2D - Sprite 在跟随物体时闪烁

unity3d - 从内容中删除/停用子元素后,ScrollRect 在固定规范化位置时无法正确更新位置

c# - 为什么我的 Razor 'RenderSection' 没有被孙 View 继承?

c# - 无法从列表中获取值以便设置网格 header 值 c#

c# - 实现 IDisposable 时在构造函数中处理异常

c# - Unity C# 中的泛型

c# - 在控制台应用程序中使用图像而不是文本

c# - 如何在 Windows 窗体中运行我的控制台应用程序代码?