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

Stopping Input.GetKeyDown...

Discussion in 'Scripting' started by AEBlob, Mar 29, 2016.

  1. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    I have a Player that uses power (for an action) each time you push Space or RightShift, however, once he uses all that power I want Space and RightShift to be deactivated until he picks up more power.

    Any Ideas? :)
     
  2. Deecann

    Deecann

    Joined:
    Mar 17, 2010
    Posts:
    93
    Hint:
    if(UserPressSpace)
    if(IHaveSomePoweLeft)
    doThis..​
    else
    doThat...​
     
  3. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53

    Ok so that's better than what I was tyrying...

    Code (CSharp):
    1.     private void HandleInput()
    2.     {
    3.         horizontal = Input.GetAxis("Horizontal");
    4.  
    5.         if (Input.GetKeyDown(KeyCode.Space) && energy.CurrentVal >= 0)
    6.         {
    7.             myAnimator.SetTrigger("transformation");
    8.             NextState(); // cycle state forwards
    9.         }
    10. [B]        else...................................[/B]
    11.  
    12.         if (Input.GetKeyDown(KeyCode.RightShift) && energy.CurrentVal >= 0)
    13.         {
    14.             myAnimator.SetTrigger("transformationback");
    15.             PreviousState(); // cycle state backwards
    16.         }
    17.     }
    ....but I'm lost as to what to do in the else statement, I don't know how to essentially disable the keys/function.
     
  4. Deecann

    Deecann

    Joined:
    Mar 17, 2010
    Posts:
    93
    Look again at my post, first condition checks is button was clicked and nothing else, if YES do the rest. In your code you are checking both things together.
     
  5. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53

    OK, so...

    Code (CSharp):
    1.     private void HandleInput()
    2.     {
    3.         horizontal = Input.GetAxis("Horizontal");
    4.  
    5.         if (energy.CurrentVal <= 0)
    6.         {
    7.             if (Input.GetKeyDown(KeyCode.Space))
    8.             {
    9.                 myAnimator.SetTrigger("transformation");
    10.                 NextState(); // cycle state forwards
    11.             }
    12.  
    13.             if (Input.GetKeyDown(KeyCode.RightShift))
    14.             {
    15.                 myAnimator.SetTrigger("transformationback");
    16.                 PreviousState(); // cycle state backwards
    17.             }
    18.         }
    19.     }
    This seems to be acting in reverse lol So it will only transform once I run out of power. However, >= 0 doesn't have the reverse effect. I feel I'm missing something painfully obvious.
     
  6. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    Ah S***...

    if ( energy.CurrentVal > 0)