2017-02-25 30 views
0

我的代碼有問題。當我按下指定的鍵碼「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]); 
     } 
    } 
} 

}

回答

0

嘗試把你的,如果(input.keydown)在更新功能,並調用它的攻擊。隨着你擁有它的方式,我相信它會每秒多次調用你的攻擊函數,因爲更新運行每一幀。還要嘗試調試currentstate的值。

編輯: 您是否在編輯器中爲currentcombotimer指定了一個值,因爲它是公開的?在腳本中它沒有起始值。在啓動函數中,您將其分配給origTimer。如果此值爲零,則您的組合時間爲零。

+0

這是因爲'Input.GetKeyDown'在'NewComboSystem()'函數中,所以**不是**問題,這個函數被'Update()'函數調用了100%。 – Programmer

+0

是的,這似乎不是問題所在。我仍然繼續嘗試,但沒有奏效。有關我的代碼有什麼問題的其他建議? –

+0

您是否在編輯器中爲currentcombotimer指定了一個值,因爲它是公開的?在腳本中它沒有起始值。在啓動函數中,您將其分配給origTimer。如果此值爲零,則您的組合時間爲零。 – Colby