【发布时间】:2017-02-01 19:46:29
【问题描述】:
我目前正在 Unity 中开发一款游戏,但无法完成某些工作。我想要这样当输入被按下 x 次(近战攻击)时,角色停止工作,直到你按下另一个按钮 x 次,即 10 次。玩家应该能够攻击,即 3 次,但是当他这样做时,角色会进入“假死”状态,无法再与玩家一起行走或近战攻击。这时候玩家应该再按10次键,然后玩家就可以再次进行近战攻击了。我以为我可以通过一个简单的 if 和 else 语句来实现这一点,但到目前为止还没有让它发挥作用。出于某种原因,我的 else 部分被立即执行,而不是在使用近战攻击 5 次后执行。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeleeCounter : MonoBehaviour {
public int attackNumber = 0;
public GameObject meleeHitbox;
// Update is called once per frame
void Update () {
if (attackNumber < 5 && Input.GetButtonDown ("Fire3"))
{
attackNumber++; // increment the counter
meleeHitbox.gameObject.SetActive (true);
Debug.Log ("Attack");
}
if (Input.GetButtonUp ("Fire3")) {
meleeHitbox.gameObject.SetActive (false);
}
else
{
GetComponent<PlayerController>().enabled = false;
Debug.Log ("Too many attacks");
// Here should come a script that if i.e. Fire4 is pressed 10 times reset attackNumer to 0; and set PlayerController to true.
}
}
}
【问题讨论】: