Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 unityGecko · Sep 09, 2016 at 06:01 PM · keybutton trigger eventskeypresskey pressedsimultaneous

Keypresses blocking each other.

I'm writing a 2D shooter in the style of Journey of the Prairie King (see: https://www.youtube.com/watch?v=MtdyVHZluAw). Currently working on the player. The player should be able to move in every direction (and diagonally) and be able to shoot in every direction (incl. diagonally) at the same time, regardless of where they are moving to.

I have defined two axes (Vertical/Horizontal) and four "Fire" buttons (FireUp, FireDown, FireLeft, FireRight) in the input manager. I assigned WASD controls to the axes and the arrow keys to the respective fire buttons.

I am using two seperate scripts for moving and shooting.

The movement script just takes the movement vector and adds it to the player's position. It also clamps the position to a boundary.

 rbody.position = new Vector3 (
     Mathf.Clamp (rbody.position.x + movementVector.x * Time.deltaTime, boundary.xMin, boundary.xMax),
     Mathf.Clamp (rbody.position.y + movementVector.y * Time.deltaTime, boundary.yMin, boundary.yMax),
     0
 ); 

The shooting script calculates the shooting direction by adding a simple direction vector of the input button to the shooting direction and passing it to a function which instantiates the bullet and adds force to it.

 direction.Set (0.0f, 0.0f, 0.0f);
 
 bool shoot = false;
 
 if (Input.GetButton ("FireUp")) {
     shoot = true;
     direction += Direction.up;
 } else if (Input.GetButton ("FireDown")) {
     shoot = true;
     direction += Direction.down;
 }
 
 if (Input.GetButton ("FireLeft")) {
     shoot = true;
     direction += Direction.left;
 } else if (Input.GetButton ("FireRight")) {
     shoot = true;
     direction += Direction.right;
 }
 
 if (shoot) {
     ShootBullet (direction);
 }

I have included only the code snippets which have to do with key presses.

My problem is the following. When I am moving the player character in a single direction or upwards diagonally, I can shoot in any other direction. When I am moving the player character downwards diagonally, I can only shoot up and down (when moving down+left) or up and left (when moving down+right).

I have noticed (using print("Something") within the if statements) that the key presses of the shooting script which do not work when moving downwards diagonally are never triggered.

Changing the code to Input.GetKeyDown() doesn't help.

What could be the problem? I can show more code if needed, I just didn't want to make this post unnecessarily longer than it already is.

Comment
Add comment · Show 2
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
avatar image doublemax · Sep 09, 2016 at 06:38 PM 1
Share

It's possible that this is not caused by your script. This could be simple key ghosting problem. Because of the way most standard keyboards work, some key combinations of more than two keys can not be detected. That's why certain "anti-ghosting keyboards" for gamers exist.

Try it out with this demo: http://www.microsoft.com/appliedsciences/content/projects/$$anonymous$$eyboardGhostingDemo.aspx

For a test, use a joystick for either movement or shooting. If it works then, you know it's not a logic error in your code.

avatar image unityGecko doublemax · Sep 09, 2016 at 08:16 PM 1
Share

Thank you so much, that seems to be the problem. I'll just have to remap the fire keys to something else then for the moment.

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.

Follow this Question

Answers Answers and Comments

68 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 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

Trigger when holding key 1 Answer

Checking if any key is up? 3 Answers

Trying to assign two functions to one key 0 Answers

Disable Key Using Javascript 0 Answers

How to use a key as trigger? 2 Answers


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