UNITY - 攻击命中组合码错误。需要帮助

问题描述:

我的代码有问题。当我按下指定的键码“Z”时,CurrentState不会更改为1并继续整个组合。我尝试了一个debug.log,发现箱子对撞机在按Z时激活,但数字不增加1.任何人都可以帮我解决吗?这是代码。UNITY - 攻击命中组合码错误。需要帮助

公共类战斗机:MonoBehaviour {

public Collider[] attackhitboxes; 
public Animator art; 
public int CurrentState = 0; 
bool activateTimertoreset = false; 
public float currentcombotimer; 
float origTimer = 50f; 
private void Start() 
{ 
    art = gameObject.GetComponent<Animator>(); 
    origTimer = currentcombotimer; 
} 
void Update() 
{ 
    art.SetInteger("currentstate", CurrentState); 
    NewComboSystem(); 
    ResetComboState(activateTimertoreset); 

} 

void ResetComboState(bool resettimer) 
{ 
    if(resettimer) 
    { 
     currentcombotimer -= Time.deltaTime; 

     if(currentcombotimer <= 0) 
     { 
      CurrentState = 0; 
      activateTimertoreset = false; 
      currentcombotimer = origTimer; 
     } 
    } 
} 
    private void LaunchAttack(Collider col) 
{ 
    Collider[] cols = Physics.OverlapBox(col.bounds.center, col.bounds.extents, col.transform.rotation, LayerMask.GetMask("Hitbox")); 
    foreach(Collider c in cols) 
    { 
     if(c.transform.parent.parent == transform) 
     { 
      continue; 
     } 
    } 
} 

void NewComboSystem() 
{ 
    if (Input.GetKeyDown(KeyCode.Z)) 
    { 
     activateTimertoreset = true; 
     CurrentState++; 
     if (CurrentState == 1) 
     { 
      LaunchAttack(attackhitboxes[0]); 
     } 

     if (CurrentState == 2) 
     { 
      LaunchAttack(attackhitboxes[0]); 
     } 

     if (CurrentState >= 3) 
     { 
      LaunchAttack(attackhitboxes[0]); 
     } 
    } 
} 

}

尝试把你的,如果(input.keydown)在更新功能,并调用它的攻击。随着你拥有它的方式,我相信它会每秒多次调用你的攻击函数,因为更新运行每一帧。还要尝试调试currentstate的值。

编辑: 您是否在编辑器中为currentcombotimer指定了一个值,因为它是公开的?在脚本中它没有起始值。在启动函数中,您将其分配给origTimer。如果此值为零,则您的组合时间为零。

+0

这是因为'Input.GetKeyDown'在'NewComboSystem()'函数中,所以**不是**问题,这个函数被'Update()'函数调用了100%。 – Programmer

+0

是的,这似乎不是问题所在。我仍然继续尝试,但没有奏效。有关我的代码有什么问题的其他建议? –

+0

您是否在编辑器中为currentcombotimer指定了一个值,因为它是公开的?在脚本中它没有起始值。在启动函数中,您将其分配给origTimer。如果此值为零,则您的组合时间为零。 – Colby