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.

Input.GetAxis vs Input.GetKey

Discussion in 'Scripting' started by emilianop, Sep 13, 2014.

  1. emilianop

    emilianop

    Joined:
    Aug 16, 2014
    Posts:
    28
    Hi all,

    I have some code that does the following:
    - move player around by changing his velocity/position
    - when player hits a button, the character stops and targets an enemy
    - by default the engine targets the closes enemy instance, but as long as in "targeting mode" the player can chose to move target to a different enemy just by moving "left/right"

    They way I do this is first by detecting whether we're in targeting mode or not:
    Code (CSharp):
    1. // if SPACE is pressed, enter scoping mode, exit when released
    2. if( Input.GetKeyDown("space") ) isScopingEnemies  = true;
    3. if( Input.GetKeyUp("space") )  isScopingEnemies  = false;
    Then I tag the corresponding enemy (closest one by default)
    Code (CSharp):
    1. // while in scoping mode, deal with enemies targeting
    2.   if( isScopingEnemies )
    3.   {
    4.   float moveHorizontal = Input.GetAxis ("Horizontal");
    5.  
    6.   if ( moveHorizontal>0.5 ) taggedEnemy++;
    7.   else if ( moveHorizontal<0.5 ) taggedEnemy--;
    8.   taggedEnemy = taggedEnemy % foundEnemyObjects.Length;
    9.  
    10.   tagAllEnemiesAtOnce( taggedEnemy );
    11.   }
    12.   else untagAllEnemies();
    As you can see, I'm using Input.GetAxis to read when the player wants to change target, and that works, but it's apparently too fast, as soon as I test this, the tag moves from enemy to enemy at insane speed, as if there was some very high inertia built into that.
    The other option would be to use GetKeyUp and check for the corresponding buttons, but that would mean I'd have to go for a specific one (WASD or arrows or both) rather than using the in built mapping mechanism.
    (I already tried with GetAxisRaw, but no luck)

    Thanks!
    e
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,186
    This is (probably) in the update loop, so its evaluating the input every frame, changing targets each time.
    KeyUp or down would definitely help, otherwise you'd have to code in something that stops retarget within a certain time frame

    tip: getAxis usually returns -1 to 1, but your statements check moveHorizontal for above/below 0.5, when really it should be around zero.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,334
    Use the GetButton functions.

    --Eric
     
  4. emilianop

    emilianop

    Joined:
    Aug 16, 2014
    Posts:
    28
    Thanks for the replies!

    hpjohn: that was just me trying to damp down the GetAxis magnitude so to trigger the even only when the stick was "fully moved" in case there was some analog trickery I wasn't aware of... but of course didn't work. Checking against 0.5 anyway doesn't do harm for fully boolean signal.

    hpjohn: I would use GetButton, but that's a bool datatype, right? Meaning I can't use the value at Input.GetButtonUp("Horizontal") to detect whether the player moved left or right, hence to switch previous or next target.
     
  5. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,351
    Difference between GetAxis and GetKey is that GetKey is kind of a boolean: If (GetKey("Up")) then do this.

    GetAxis is identical but in this case it's a number from between -1 and 1. You can do this:

    Code (JavaScript):
    1. function Update() {
    2.     HorSpeed = 10 * Input.GetAxis("Horizontal");
    3.     VerSpeed = 10 * Input.GetAxis("Vertical");
    4. }
    If you want to achieve a smooth movement + smooth slowing down over time - then go with Input.GetAxis()
    Input.GetAxis() is awesome if you want to add controller support!

    If you want an immediate movement then go with Input.GetButton()

    Hope this makes sense! : P
     
    eyezack45 likes this.
  6. emilianop

    emilianop

    Joined:
    Aug 16, 2014
    Posts:
    28
    Makes total sense thanks, but I was wondering about GetButton.
    In order to have this working I realized I need to set up in the Inputs project settings a couple of new entries for "left" and "right".
    This is because I don't want to map this to a specific single key, I want to associate it to a direction, the same way GetAxis does (and then in the preferences you specify that -say- the Horizontal axis is LEFT-RIGHT and A-D and joystick movements).

    I couldn't find any way to do so, maybe I'm missing something?

    Cheers,
    e
     
  7. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,351
    There is no need to setup Inputs for GetButton. You can, but there is simpler way:
    Code (JavaScript):
    1. function Update() {
    2.     if (Input.GetButton(KeyCode.Up)) {
    3.         //move forward
    4.     }
    5. }
    Refer to: http://docs.unity3d.com/ScriptReference/KeyCode.html
     
    emilianop likes this.
  8. emilianop

    emilianop

    Joined:
    Aug 16, 2014
    Posts:
    28
    A-HA!

    That's the missing piece I was looking for!!
    Thanks elmar1028!
     
  9. JediEmma

    JediEmma

    Joined:
    Jan 27, 2014
    Posts:
    230
    I couldn't help but notice when you check the GetAxis value against 0.5f twice, the second 0.5f needs to be -0.5f, then it wouldn't be SO quick! ;)
     
    emilianop likes this.
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,334
    That wouldn't compile, and is the opposite of what GetButton is intended for. What you wrote is how Input.GetKey works. Input.GetButton requires a string, and works with the input manager, like GetAxis does. Therefore you need to set up inputs, which avoids hard-coding specific keys. (Hard-coding is bad because different countries have different keyboard layouts, some people are left-handed and right-handed keys can be awkward, etc.)

    Yes, but if you can use GetKey, then you can use GetButton. It's the same thing except you set it up in the input manager, which is exactly what you said you wanted.

    --Eric
     
  11. emilianop

    emilianop

    Joined:
    Aug 16, 2014
    Posts:
    28
    Correct, as a matter of fact I did it this way in the end after noticing that fails to compile.
    Thanks again,much clearer now.
     
unityunity