Search Unity

Timer counting problem

Discussion in '2D' started by drpelz, Jul 18, 2018.

  1. drpelz

    drpelz

    Joined:
    Dec 7, 2017
    Posts:
    69
    I’d like to show some text when the player kills e.g. 3, 4, or 5 enemies in a given time (in Unity3d).

    I have a timer (comboTimer) which is in my PlayerControl-script that starts when an enemy gets shot by the player. When the timer starts a counter (comboCounter) gets increased everytime an enemy gets shot by the player. The other script (EnemyControl) controls the way how the appropriate text gets displayed when the kill-counter (comboCounter) reaches a certain value (e.g. 3 kills = “Combo x3”). This should be done within the timer’s value.

    The problem is the text that gets shown is in almost most cases 3 kills (“Combo x3”). I never reach 4 or 5 kills within the timer’s value even when I increase the waitTime to e.g. 5 or 10 seconds. There is a bug within the logic but can’t find it. Can someone help me, please?

    This is the Timer of my PlayerControl-script:
    Code (CSharp):
    1. public static bool comboTimerRunning = false;
    2. public static int comboCounter;
    3.  
    4. float comboWaitTime = 1f;//1.0 Sekunden
    5. float comboTimer;
    6.  
    7. public void ComboTimer() {
    8.    comboTimer += Time.deltaTime;
    9.    if (comboTimer > comboWaitTime)
    10.    {
    11.        ResetComboTimer();
    12.    }
    13. }
    14.  
    15. public void ResetComboTimer () {
    16.    comboTimer = 0f;
    17.    comboTimerRunning = false;
    18.    comboCounter = 0;
    19. }
    This is the script that controls the handling of the displayed text (EnemyControl):
    Code (CSharp):
    1. if (!PlayerControl.comboTimerRunning) {
    2.        PlayerControl.comboTimerRunning = true;
    3. }
    4. PlayerControl.comboCounter++;
    5. if (PlayerControl.comboCounter >= 3) {
    6.    if (PlayerControl.comboCounter == 3) {
    7.            comboTxtObj.GetComponent<ComboTxt>().ShowComboText("Combo x3");
    8.    } else if (PlayerControl.comboCounter == 4) {
    9.            comboTxtObj.GetComponent<ComboTxt>().ShowComboText("Awesome");
    10.    } else if (PlayerControl.comboCounter >= 5) {
    11.            comboTxtObj.GetComponent<ComboTxt>().ShowComboText("Mega-Kill");
    12.    }
    13.    PlayerControl.comboTimerRunning = false;
    14. }
     
  2. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    When are you calling ComboTimer()? When is the second piece of code (in your EnemyControl) called?