我正在 Unity 中制作回合制角色扮演游戏,我有一个滑块,当玩家攻击时会弹出。 A 沿着滑块运行,玩家让滑块越靠近中心并按下 Z,它造成的伤害就越大。然而,由于某种原因,它不是一个简单的输入,你要么必须粉碎 Z,要么它根本不会接受输入。
这是代码:
void DamageGauge(int target, float power, bool special, string status, string name)
{
int t = target;
float p = power;
bool s = special;
string st = status;
string n = name;
meterOpen = true;
slashSlider.gameObject.SetActive(true);
slashSlider.value = 0;
StartCoroutine(DamageSliderTick(t, p, s, st, n));
}
// ReSharper disable Unity.PerformanceAnalysis
public IEnumerator DamageSliderTick(int target, float power, bool special, string status, string name)
{
bool press = false;
int t = target;
float p = power;
bool s = special;
string st = status;
string n = name;
BattleChar sTarget = activeBattlers[t];
for (int i = 0; i < 51; i++)
{
yield return new WaitForSeconds(.01f);
slashSlider.value += 4;
if (Input.GetKeyDown(KeyCode.Z) || i == 50)
{
float v = slashSlider.value;
if(i != 50)
{
switch (v)
{
case < 20:
DealDamage(t, p, .5f, s, st, n);
break;
case < 50:
DealDamage(t, p, .8f, s, st, n);
break;
case < 85:
DealDamage(t, p, 1, s, st, n);
break;
case < 97:
DealDamage(t, p, 1.4f, s, st, n);
break;
case < 132:
DealDamage(t, p, 1, s, st, n);
break;
case < 170:
DealDamage(t, p, .8f, s, st, n);
break;
case < 200:
DealDamage(t, p, .5f, s, st, n);
break;
}
} else
{
DealDamage(t, p, .5f, s, st, n);
}
slashSlider.gameObject.SetActive(false);
slashSlider.value = 0;
if (activeBattlers[t].currentHP <= 0)
{
RewardDialog.instance.xpPool += sTarget.xpWorth;
RewardDialog.instance.moneyPool += sTarget.dollarWorth;
if (sTarget.itemDrop[0] != null)
{
RewardDialog.instance.ShuffleItems(sTarget.itemDrop);
}
}
UpdateStatus(t);
NextTurn();
break;
}
}
}
Input.GetKeyDown
才会返回 true。因为您在 WaitForSeconds
之后调用了它,所以不能保证您只是在该帧按下该键。
一般情况下,可以在
Input.GetKeyDown
事件方法中调用Update
来记录按键按下的时间,然后在协程中比较当前时间和按下时间的差值。如果小于某个阈值,则认为玩家在此期间按下了按键。
// Update
void Update()
{
if (Input.GetKeyDown(KeyCode.Z))
zPressedTime = Time.time;
}
// DamageSliderTick
if (Input.GetKeyDown(KeyCode.Z)
|| (Time.time - zPressedTime < threshold)
|| i == 50)