我目前正在Unity工作,我无法完成任务。我想要它,以便当按下输入x次(近战攻击)时,角色停止工作,直到你按下另一个按钮x次,即10次。玩家应该能够攻击3次,但是当他这样做时,角色会进入“假死亡”状态。状态,他不再能够与玩家一起行走或进行近战攻击。那时玩家应该击中另一个键10次,然后玩家将能够再次进行近战攻击。我以为我可以通过一个简单的if和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.
}
}
}
答案 0 :(得分:3)
似乎你在某种程度上混淆了你的情况。正如它当前编写的那样,只要Input.GetButtonUp ("Fire3")
为假(即,每当Fire3尚未释放),您的代码将执行else块,无论玩家攻击多少次;你写的两个if语句是相互独立的。
else语句应该附加到attackNumber
,而不是Input.GetButtonUp ("Fire3")
的结果。此外,您可能希望在attackNumber
更新后立即禁用播放器脚本。
这里的代码有点乱窜,应该更接近你想要完成的事情:
void Update () {
// Only bother checking for Fire3 if attacks can still be made
if (attackNumber < 5)
{
if (Input.GetButtonDown ("Fire3"))
{
attackNumber++; // increment the counter
meleeHitbox.gameObject.SetActive (true);
Debug.Log ("Attack");
// Detect when too many attacks are made only if an attack was just made
if (attackNumber == 5) {
GetComponent<PlayerController>().enabled = false;
Debug.Log ("Too many attacks");
}
}
}
// If attacks can't be made, then check for Fire4 presses
else
{
// Here should come a script that if i.e. Fire4 is pressed 10 times reset attackNumer to 0; and set PlayerController to true.
}
// Allow disabling of the hitbox regardless of whether attacks can be made, so it isn't left active until after the player is enabled again
if (Input.GetButtonUp ("Fire3")) {
meleeHitbox.gameObject.SetActive (false);
}
}
希望这有帮助!如果您有任何问题,请告诉我。