Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Getting the actual key of Input.GetAxis

Discussion in 'Scripting' started by gameboyyo, Apr 8, 2012.

  1. gameboyyo

    gameboyyo

    Joined:
    Jul 16, 2011
    Posts:
    20
    Please, i'm looking to add interaction to a gameobject, and i would like a screen message to appear when the mouse is over it. The mouse over part is all right, as is the gui part, however i have a problem with the message.

    My problem is that i have a keyboard remap function, and i would like the user to actually see his mapped key in the message, for example, since i've created an 'Interact' axis and mapped it to the u key, i would like the following message to appear

    Press "u" to interact.

    but there doesn't seem to be a 'value' for axis?

    how do you make a "press u to interact" instead of "press Interact to interact"? Can't expect that the user remembers the name of the axis instead of just knowing the key he needs to press to do something!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  3. taz030485

    taz030485

    Joined:
    Feb 2, 2012
    Posts:
    10
    May I ask why you are using GetAxis() for single keys and not just GetButton()?
    And if your game is PC/MAC based you could also use GetKey() and pass in the Keycode for the key you want.

    Code (csharp):
    1. if (Input.GetKey(KeyCode.U))
    2. {
    3.     //
    4. }
    or even

    Code (csharp):
    1. KeyCode interact = KeyCode.U
    2.  
    3. if (Input.GetKey(interact))
    4. {
    5.     //
    6. }
    that way you can configure what key is the interact key

    NOTE: I have not tested this code, it is purely made up from memory, if it doesn't work let me know and I'll put some effort in and make it work
     
  4. gameboyyo

    gameboyyo

    Joined:
    Jul 16, 2011
    Posts:
    20
    omfg your idea is from 2009! Thanks for shedding light into this. I honestly think its ridiculous that this is not working as it should.

    Regarding your idea, taz, it seems i will need to follow that road. I just wanted that my users would be able to redefine their keys :(
     
  5. taz030485

    taz030485

    Joined:
    Feb 2, 2012
    Posts:
    10
    You can use GetButton() and have the user redefine the buttons using the unity input manager, but then u run into your original problem of being able to use the defined key in your text.

    In my head I can think of a way of doing it using GetKey() that would involve a script with whole bunch of GetKey() calls for each key to redefine a function and also map a KeyCode to a character/string for use in text. If I have some spare time tomorrow I can try and whip something up if you'd like.
     
  6. Morning

    Morning

    Joined:
    Feb 4, 2012
    Posts:
    1,141
    Use GetKey(), assign each action to a keycode like walk = keycode.this...
    After that use a text file or any other file to store your keys in a config. Then in the game make a key configurer so user can redefine keys. If you feel like it, you can also use serialization. Though I have no experience in that.
    Store keys in text, then read when game starts. Rewrite the text if player changes the config (but store a backup of default keys to revert back). You can also convert string to enum so there is no problem.
    This is what I do for my game and it works fairly well. Might sound complicated at first but in the end it's so much better than the default input manager that gives you very little access.
     
  7. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    There are some valid reasons to use Axis over GetKey. With axis you can
    a) Wet up senstivity and gravity, this way you can simulate slowly accelerating/decelerating (i.e. if the user hits it, it starts at 0.2, and grows slowly up to 1.0) and don't get instantly up.
    b) You can have alternative key mappings for the same thing, i.e WASD + Arrow keys
    c) You can have multiple input devices doing the same thing (i.e. movement via mouse axis, via joystick and via keyboard with the same line of code: Input.GetAxis("vertical");
    d) You don't have to bother making your own input manager to allow changing bindings
     
    fradd and Bencarbon like this.
  8. taz030485

    taz030485

    Joined:
    Feb 2, 2012
    Posts:
    10
    I know there are reasons to use GetAxis() but I was referring directly to gameboyyo's use.
    And if you read the first line you quoted I was asking why use GetAxis() instead of GetButton() not GetKey(), GetButton() also does b,c and d of your list.
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It does a, b, c and d actually. Indeed there's no reason to use GetAxis for a single button when you should be using GetButton instead. Using GetAxis for single buttons makes the input manager front-end confusing. GetAxis is for axes....

    --Eric
     
  10. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    ???

    Can you elaborate on 'a' plz.
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Oops...no...was thinking of a custom input routine I wrote a while ago.

    --Eric
     
  12. Mirzero

    Mirzero

    Joined:
    Jul 7, 2012
    Posts:
    9
    I think you guys have diverted the question. It seems to me the core question is:

    How do I get the name of the actual key to which a particular action is bound? i.e. If I have "mySuperAction" bound to 'u', how do I programatically get 'u'?
    I presume Eric's answer of "you can't" is accurate... but I'm really hoping it isn't! Is there no way to manage this, other than wrapping the built-in input manager inside our own that keeps track of the bindings?
     
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  14. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    There are a couple currently on the Asset Store, though they don't offer complete control over input (one is missing axes, and I think the other is too, but it's been a while since I checked). One is called cInput, and I can't recommend the other.

    I'm not certain whether they allow access to the keys - I'd assume they at least include their source code so you can make the keys visible to your code, if they aren't by default.

    There's also "CustomInput", made by me. It's not on the store yet, but you can take a look at the demo at that link and see if that's what you're looking for. CustomInput does make keys visible to your code by default. ^_^ I built CustomInput specifically to address the "I can't tell what keys are" and "Why does my user have to restart the game to change controls" issues.
     
    Last edited: Oct 17, 2012