当用户按下相同的底部多次攻击将改变时,我试图在单位中进行3种不同的攻击。 我尝试了这个代码没关系,但未能跟踪攻击次数
<Build Include=
然后我添加else并将attTraking设置为0,代码无法退出第一个IF语句。
答案 0 :(得分:1)
你还没有完整的课程,所以很难说。
然而,我的猜测是attTraking
每次都重置为零,因为
如果你添加一个像这样的else语句:
else
{
attTraking = 0;
}
然后在没有按下“Q”键的任何更新时,attTracking将重置为零(这会使您的“多次按键”的逻辑完全失效
答案 1 :(得分:1)
Pac0的回答对你的情况来说是正确的,但我只是想插话并说有更好的方法来做这件事。
int maxCount = 2;
int attCount = 0;
public int[] AttackTriggers; //Add the trigger ID's to the array via the editor.
public float attackResetTime = 1.0f; //Will take one second to reset counter.
if(Input.GetKeyDown(KeyCode.Q))
{
anim.SetTrigger(AttackTriggers[attCount]);
attCount++;
if(attCount > maxCount)
{
attCount = 0;
}
}
但是如果用户在指定的时间内没有按Q,您需要检查是否应该将attCount重置为0。这可以通过Coroutines来实现。
public IEnumerator ResetAttCount()
{
yield return new WaitForSeconds(attackResetTime);
attCount = 0;
}
现在,我们可以根据时间重置计数,但我们仍然需要确保在正确的位置启动和停止协程,所以让我们稍微修改一下原来的if语句:
if(Input.GetKeyDown(KeyCode.Q))
{
StopCoroutine(ResetAttCount());
anim.SetTrigger(AttackTriggers[attCount]);
attCount++;
if(attCount > maxCount)
{
attCount = 0;
}
}
然后让我们为Key Up事件添加一个声明:
if(Input.GetKeyUp(KeyCode.Q))
{
StartCoroutine(ResetAttCount());
}
现在没有我们通过在释放钥匙时启动计时器来成功计时重置攻击计数,并在一秒钟内再次按下钥匙后停止计时。