• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Pafk · Feb 02, 2016 at 01:33 PM · controlsmouseclickkeypressgame design

Elegant Solution to the Problem of Dealing With Multiple Key/Mouse Presses at the Same Time

I am working on a game concept but before going any further I want to make sure that I do not make any bad choices regarding the control schema. One of the central ideas that the game revolves around is being able to combine character actions into other actions based on what their single key press actions are. One of the issues is recognizing what the player intends. This issue can, from my point of view, be broken down into two issues: synchronicity of presses and choosing what action to trigger.

For synchronicity there are only really two options that I've been able to come up with on my own and it seems that the only real differences between the two options are from a game design point of view.

  1. After the first key press set a timer in motion for some small amount of time, (1/10th of a second? I don't know, I would need to test manually/ask best practices about this amount of time). After that time has passed gather the key combination that is pressed down. Don't start the countdown again until one of the used keys has been released.

  2. From no keys pressed set a timer in motion. After that time has passed gather the key combination that is pressed down, from here if any changes are made to the key combination check if its a valid change and move to the action specified from that key combination.

  3. (Only occurred to me after first posting the question) Only look up what keys are being pressed every 0.1 (or better measure) seconds.

For making sure I direct the game to do what the user intends, however, I have come up with a few more ideas.

First all of the following options are built on the following ideas. First, each possible action, compound or simple has its own class, each class contains it own logic and a list of the keys needed to activate it. Second keys can be separated into mobility keys and action keys. Mobility keys are simple enough that they don't need a complex structure to hold the logic, their logic can be run inline with each item of mobility logic having the same effect regardless of the rest of mobility logic. Each action class has a isMobilityBlocking check to see whether it cancels out the mobility logic in its entirety.

From these assumptions here are the possibilities I've considered for narrowing down from the ability list.

  1. A simple array/list of action objects picked through by simple a simple key checking logic that checks each action class' key presses. Why I considered it: It stops too much information from being held in one file Why I'm hesitant: It would create too much processing just to parse key presses, it would have to check every single ability

  2. A tree of action objects where each level represents a different key that can be pressed and the children are yes and no. Why I considered it: It would be much faster than the array to parse through Why I'm hesitant: It would put a lot of information into one file. This approach is not collaboration friendly, if two separate contributors tried to add a new key into the mix separately merging the changes would not be pretty. Each null combination by necessity must by explicitly spelled out

  3. A dictionary/hash/whatever you call it in the language of action objects where the key is constructed based on the key combination of the action. Why I considered it: It brings the time to find the ability down immensely, reintroduces collaboration friendliness. Why I am hesitant: The logic of creating the key builder could be finicky and might break down.

  4. An array again, but this time each key is given a value of 0-x and an integer is created using 2^keyvalue sums for the action's array index. Why I considered it: Uses fewer data structures Why I am hesitant: It adds more logic to the code, more points of failure while providing little speed improvement over #3. Reintroduces manually nulling empty key combinations.

All of these considerations have been made with the assumption that the performance of inserting into the data structure is negligible compared to the performance of the repeated searches that will be necessary in the context of a game. If there is anything I missed or a better option advice would very much be appreciated.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

39 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 5 2D How to make a cursor move with keys? 0 Answers

Match 3 Movement Using Keyboard Controls,Match 3 movement using keyboard controls 1 Answer

Unable to get mouse or key events in ExecuteInEditMode 0 Answers

On click interactions control 1 Answer

How to make Rotation of HingeJoint2D constant via mouse button C# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges