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.
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.
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.
Your answer
Follow this Question
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