我是 Unity 新手和初学者程序员。我尝试了 Unity 的 GUI(画布、按钮、标签等)。
对于按钮,我创建了一个 ButtonClickScript 类,这样当我点击按钮时,变量 waterBucketAmnt 会增加,直到达到 3。然后我决定将我的实验再进一步,所以我创建了一个非常简单的人动画倾倒一桶水。我想限制这个人使用 waterBucketAmnt 倾倒水的次数。
我可以点击 GUI 按钮 3 次,一切正常。int waterBucketAmnt = 3。然后我开始游戏并按下键盘上配置的“Fire3”动作,它完全符合预期。问题是我可以在不到一秒的时间内连续点击“Fire3”动作,因此 waterBucketAmnt = 0 并且动画只播放一次。如何延迟输入直到动画完成?
using UnityEngine;
using System.Collections;
public class waterBucketAction : MonoBehavior {
Animator waterAnim;
// a simple player controller using unity's RigidBody2D feature. I added PlayerControllerScript so that I could make sure the player had to remain on the ground in order to dump the bucket of water. checkGround is so that I could inherit the grounded gameObject I created.
PlayerControllerScript checkGround;
public ButtonClickScript checkWaterAmnt;
void Start () {
checkGround = FindObjectOfType <playerControllerScript> ();
waterAnim = GetComponent<Animator> ();
}
void FixedUpdate() {
waterAnim.SetBool ("isPlaying", false);
if (checkWaterAmnt.waterBucketAmnt > 0 && !waterAnim.GetCurrentAnimatorStateInfo(0).IsName("waterBucketAnimation")) {
if (checkGround.grounded == true && Input.GetButtonDown ("Fire3")) {
waterAnim.SetBool("isPlaying", true);
//I need to wait for the animation to complete or delay the input. I searched multiple places and the best answer I could find was Animation.IsAnimating, but that seems to be deprecated? The API says that Animator replaced Animation, but I couldn't find an equivalent method.
if(waterAnim.GetCurrentAnimatorStateInfo(0).IsName("waterBucketAnimation"))
checkWaterAmnt.waterBucketAmnt -=1;
}
}