我创建了一个名为 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
而不是 GetButtonDown
或 GetButtonUp
。
从文档中,GetButton
:
Returns true while the virtual button identified by buttonName is held down.
因此,如果您按住空格键两帧(即使您按下了一次,物理 Action 所需的时间也比一帧长),那么它会触发两次。如果您按住它的时间超过此时间,它会继续发射。
如果您改用 GetButtonDown
或 GetButtonUp
,它应该只触发一次,即在注册按下的确切帧。
Returns true during the frame the user pressed down the virtual button identified by buttonName.
关于c# - Unity动画播放两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32633642/