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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Input - How to check for inactive input during a game?

Discussion in 'Scripting' started by iRyan, Oct 23, 2015.

  1. iRyan

    iRyan

    Joined:
    Jan 16, 2013
    Posts:
    20
    How do you check during play time if the user is pressing the button?

    Currently I'm trying to do a game where the user has to keep pressing the button to move but when he stops pressing the button, the speed will start dropping due to inactivity.

    Code (CSharp):
    1. if (Input.GetKeyDown("up")){
    2.      speed += 1;
    3. }
     
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    GetKeyDown is only called once. Use GetKey intstead.

    Example :

    Code (CSharp):
    1. if(Input.GetKey("up")){
    2. speed += 1;
    3. }
    4. else{
    5. speed -= 1;
    6. }
     
  3. iRyan

    iRyan

    Joined:
    Jan 16, 2013
    Posts:
    20
    Hi, I do not wish for the user to hold down the button which is why I used GetKeyDown instead but thanks for replying.
    Currently I wish to track for inactive button usage but I do not know how would you know of way to track whether the button is actively being pressed?
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Input.GetKey tracks if it's actively down. If it's false, it's not pressed.

    It goes this:
    Input.GetKey - returns true if the key is in the down positin, otherwise returns false
    Input.GetKeyDown - returns true if last frame 'GetKey' was false, but this frame it's true
    Input.KeyKeyUp - returns true if last frame 'GetKey' was true, but this frame it's false
     
  5. the_motionblur

    the_motionblur

    Joined:
    Mar 4, 2008
    Posts:
    1,774
    IF you want to check whether the key is currently pressed down you should consider using GetKey for checking. Maybe use it just as a secondary check to see if the key is currently still pressed or use a timer to Count down when GetKeyDown is pressed?
     
  6. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    Maybe you can try:

    Each time the key is pressed down, set a movement force. Each frame, this force will be applied to keep the player moving. But drain from it a little bit each frame as well. You'll get a force and a slow down after. In order to keep the force back up, the user has to keep resetting it by pressing the key (you can cap it at a certain value if needed).
     
  7. iRyan

    iRyan

    Joined:
    Jan 16, 2013
    Posts:
    20
    @Intense_Gamer94 @lordofduct @the_motionblur @JasonBricco

    Hi guys thanks for your replies, I've managed to do something simple.
    Code (CSharp):
    1.     double time = 0;
    2.     float afkTime = 2;
    3.  
    4.         if (Input.GetKey("up") == false){
    5.             time += Time.deltaTime;
    6.             if (time > afkTime){
    7.                 agent.speed -=1;
    8.                 time -= time;
    9.             }
    10.         }