Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Button requires to be pressed twice on first load to register

Discussion in '2D' started by Diluision88, May 18, 2020.

  1. Diluision88

    Diluision88

    Joined:
    Apr 5, 2020
    Posts:
    10
    I have a script which controls player movement and all works fine except for my dash mechanic. When the script first loads the first input of the "Dash" button does nothing but then works perfectly after that, it is as if though the first button press is not being registered. I will add the affected code below. Thank you.

    Code (CSharp):
    1. if(direction == 0)
    2.         {
    3.             if (Input.GetButtonDown("Dodge") && !dodged && (grounded || airDodgeObtained))
    4.             {
    5.                 if(moveX < 0)
    6.                 {
    7.                     direction = 1;
    8.                 }
    9.                 else if (moveX > 0)
    10.                 {
    11.                     direction = 2;
    12.                 }
    13.             }
    14.         }
    15.         else
    16.         {
    17.             if(dodgeTime <= 0)
    18.             {
    19.                 direction = 0;
    20.                 dodgeTime = startDodgeTime;
    21.                 playerRB.velocity = Vector2.zero;
    22.             }
    23.             else
    24.             {
    25.                 dodgeTime -= Time.deltaTime;
    26.  
    27.                 if (direction == 1)
    28.                 {
    29.                     playerRB.velocity = Vector2.left * dodgeSpeed;
    30.                     dodged = true;
    31.                     dodgeCooldown = startDodgeCooldown;
    32.  
    33.                     if (dodgeJumpUnlocked)
    34.                     {
    35.                         canDoubleJump = true;
    36.                     }
    37.                 }
    38.                 else if (direction == 2)
    39.                 {
    40.                     playerRB.velocity = Vector2.right * dodgeSpeed;
    41.                     dodged = true;
    42.                     dodgeCooldown = startDodgeCooldown;
    43.  
    44.                     if (dodgeJumpUnlocked)
    45.                     {
    46.                         canDoubleJump = true;
    47.                     }
    48.                 }
    49.             }
    50.         }
    51.  
    52.         if (dodged)
    53.         {
    54.             dodgeCooldown -= Time.deltaTime;
    55.  
    56.             if(dodgeCooldown <= 0)
    57.             {
    58.                 dodged = false;
    59.             }
    60.         }
     
  2. DiegoDePalacio

    DiegoDePalacio

    Unity Technologies

    Joined:
    Oct 28, 2009
    Posts:
    506
    Hi @Diluision88,

    This can be happening for example if your app does not have the focus when you try to click the button for the first time.

    If not, please provide more information about the concrete situation in where you're having the issue, like if it's happening on the `Game` view in the Editor or in a build.


    Good luck with it!
     
  3. Diluision88

    Diluision88

    Joined:
    Apr 5, 2020
    Posts:
    10
    Hi, the issue occurs both in the 'game' view as well as in a build. All other button presses occur correctly from start apart from pressing my "dodge" button which takes a double press the first time to work, then after the initial first "null" press it works perfectly.
     
  4. Diluision88

    Diluision88

    Joined:
    Apr 5, 2020
    Posts:
    10
    Nevermind I figured it out, I wasn't setting : dodgeTime = startDodgeTime in start function so it required a button press to set its value before allowing the script to continue. Thanks anyway
     
    DiegoDePalacio likes this.