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. Dismiss Notice

Checking for input in Update() logs only once

Discussion in 'Editor & General Support' started by Jahni-Slim, Jul 23, 2018.

  1. Jahni-Slim

    Jahni-Slim

    Joined:
    May 7, 2013
    Posts:
    6
    Title says it, I know this is basic; I'm just getting back into Unity. Either way, this code is in a script which is connected to the player GameObject that is forever in the scene. When I click or press 'd' the console logs my
    Debug.Log()
    once and only once even when I hold them down. I can't figure out why.

    Code (CSharp):
    1. void Update () {
    2.         if (Input.GetKeyDown(KeyCode.D))
    3.         {
    4.             Debug.Log("Key Press");
    5.         }
    6.  
    7.         if (Input.GetMouseButtonDown(0))
    8.         {
    9.             Debug.Log("Touch happening");
    10.         }
    11.     }
    Thanks!
     
  2. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    353
    Input.GetKeyDown is only active the first frame you press the button; if you want the user to hold the button, you'd use Input.GetKey instead. The other input methods work similarly.
     
    Jahni-Slim and Issun like this.
  3. Issun

    Issun

    Joined:
    Nov 7, 2013
    Posts:
    17
    To further expand on what Philip has said; (I think its always good to hear an explanation from two different people)

    If you want to check if a key is currently being held down: use Input.GetKey.
    If you want to check if a key is being pressed down on that frame use Input.GetKeyDown.
    And if you want to check if a key is being released on that frame use Input.GetKeyUp.
    The same goes for the Mouse button methods too, good luck!
     
    Jahni-Slim and Philip-Rowlands like this.
  4. Jahni-Slim

    Jahni-Slim

    Joined:
    May 7, 2013
    Posts:
    6
    Thanks my friends!