Search Unity

How can I check how many keys are being pressed?

Discussion in 'Scripting' started by MartinIsla, Dec 18, 2015.

  1. MartinIsla

    MartinIsla

    Joined:
    Sep 18, 2013
    Posts:
    104
    Hi! I'm developing a game that requires the player to be smashing the keys. It works just fine, but the thing is some keyboards are better than others, so players with better keyboards have more possibilities of winning or getting a highscores than others.

    I wanted to fix this by not letting the player to press more than 4 keys at the same time.

    Is this possible? Maybe there's another way to do it?

    Thanks a lot!
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Well, I'm assuming this is an issue because you're scoring the number of keypresses.

    Don't score more than 4 per update.
     
  3. MartinIsla

    MartinIsla

    Joined:
    Sep 18, 2013
    Posts:
    104
    No, sorry, that's not it!
    There are two random keys in your keyboard that deactivate a nuclear reactor and you must find them.
    Here's the link to the game in case I'm not explaining myself: http://gamejolt.com/games/react/113822
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    OK... so you have a game that requires you to find the 2 keys to deactivate the nuclear reactor.

    But it can be cheated by just banging on the keyboard. And if your keyboard is "better", you can bang on more keys at once.

    How about creating a game mechanic that thwarts the players ability to do that... like maybe there are 2 random keys that if pressed causes the nuclear reactor to melt down faster.
     
    eisenpony likes this.
  5. MartinIsla

    MartinIsla

    Joined:
    Sep 18, 2013
    Posts:
    104
    We are working on that, but this is supposed to be a game mode that relies mostly on luck
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    OK... well, being able to press multiple keys at once isn't an all the time issue.

    Now, I don't know what your code is... but I'ma take a guess that you just generate a random pair of keycodes, and every update you check if they're being pressed, and if both are being pressed the game wins.

    If they're pressing 8 keys, and none of them are the win keys, it really doesn't matter. Because they're not winning anyways. Right?

    So how about, when the win state test is true (both keys are being pressed), you then cycle through all other keys, and see if anything else is down (just loop over all keycodes... unfortunately unity doesn't give a way to list off currently held keys). This may be slower, but you only do it if they're in a win state, because it doesn't matter otherwise.
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Also, if this is a game mode that relies mostly on luck... and you're allowing 4 or so keys at the same time (even though the win state is 2), who cares if it's 4 or 8.

    It's luck!

    Having a shorter time than someone else at luck, is like being lucky to have rolled 'snake-eyes' on first throw. It shows no level of skill, and only the ability to have been lucky enough to enter the win state first.

    If its a system so easily cheated, its a game mechanic that probably needs to be rethought out. I honestly don't see the fun of sliding my fingers back and forth over my keyboard to trip the random 2 keys that your game is looking for.
     
  8. MartinIsla

    MartinIsla

    Joined:
    Sep 18, 2013
    Posts:
    104
    Your solution is good! I'm working on it.

    And we know about that mechanics problem. This game was made for Ludum Dare and, as I said, we are working in other game modes that require more strategy, but these guys like the game!

    http://ludumdare.com/compo/ludum-dare-34/?action=preview&uid=54812