Search Unity

Input.getkey is constantly being pressed ( beginner )

Discussion in 'Navigation' started by truesuckapunch, Dec 6, 2017.

  1. truesuckapunch

    truesuckapunch

    Joined:
    Dec 6, 2017
    Posts:
    3
    [Solved]
    So I'm just currently trying to master the basics of movement but currently having issues with my player movement. I set Input.GetKey and also the key it's bind to to show up in the debug log. Currently its being pressed even tho I'm not pressing the key. I unplugged my keyboard and any other devices that could cause the issue.

    Code (CSharp):
    1. public class playermovement : MonoBehaviour {
    2.  
    3.     public Rigidbody rb;
    4.     public float forwardForce = 1000;
    5.  
    6.     void FixedUpdate () {
    7.         rb.AddForce(0, 0, forwardForce * Time.deltaTime);
    8.  
    9.         if ( Input.GetKey("d"));
    10.  
    11.         {
    12.             Debug.Log("pressed d");
    13.             rb.AddForce(500 * Time.deltaTime, 0, 0);
    14.  
    15.         }
    16.  
    17.         if ( Input.GetKey("a"));
    18.         {
    19.             Debug.Log("pressed a");
    20.             rb.AddForce(-500 * Time.deltaTime, 0, 0);
    21.  
    22.         }
    23.     }
    24. }
    edit: solved thanks to LaneFox
     
    Last edited: Dec 6, 2017
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,511
    Use Input.GetKeyDown(KeyCode.D);

    Code (CSharp):
    1. public class playermovement : MonoBehaviour {
    2.  
    3.     public Rigidbody rb;
    4.     public float forwardForce = 1000;
    5.  
    6.     void FixedUpdate ()
    7.     {
    8.         rb.AddForce(0, 0, forwardForce * Time.deltaTime);
    9.  
    10.         if (Input.GetKeyDown(KeyCode.D));
    11.         {
    12.             Debug.Log("pressed d");
    13.             rb.AddForce(500 * Time.deltaTime, 0, 0);
    14.  
    15.         }
    16.  
    17.         if (Input.GetKeyDown(KeyCode.A));
    18.         {
    19.             Debug.Log("pressed a");
    20.             rb.AddForce(-500 * Time.deltaTime, 0, 0);
    21.  
    22.         }
    23.     }
    24. }
     
    kjyuu and truesuckapunch like this.
  3. truesuckapunch

    truesuckapunch

    Joined:
    Dec 6, 2017
    Posts:
    3
    The code works but I'm still having the issue that unity is saying I'm pressing " a" and " d" over and over in the debug even tho I didn't even touch the keys.

    edit: https://gyazo.com/8de1425a5b0a2149a92e0cba5c757fd5
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,511
    lol, okay.

    Remove the semicolon's at the end of the if statements.
     
    kjyuu and truesuckapunch like this.
  5. truesuckapunch

    truesuckapunch

    Joined:
    Dec 6, 2017
    Posts:
    3
    omg That was the issue. Thank you so much!
     
    kjyuu likes this.
  6. MirrorO_O

    MirrorO_O

    Joined:
    Feb 6, 2022
    Posts:
    1
    Bro thank you so much i could even tell you how long it has taken me to fix that, idc if this is necro you are a savior