- Home /
Why can't i firing a bullet when holding down left and up arrow keys by pressing space?
I have coded a small game where you are stationary in the middle of the screen and you can hold down left arrow to fire left, up arrow to fire up, right arrow to fire...ETC
while i recently coded it that if you are holding both the up arrow and the right arrow you fire diagonally, every direction works (up/right, down/right, down/left) EXCEPT left/up
i wrote in a couple print statements and the game is reading me pressing down both the left and up arrow but the print statement within the firing code will not print so for some reason unity is no registering me pressing the space key when i have both the up and left arrow keys pressed....any help!?!?!!?
if (Input.GetKey("right")){
transform.Translate(Vector3.right * playerSpeed * Time.deltaTime);
}
if (Input.GetKey("left")){
transform.Translate(Vector3.left * playerSpeed * Time.deltaTime);
}
if (Input.GetKey("up")){
transform.Translate(Vector3.forward * playerSpeed * Time.deltaTime);
}
if (Input.GetKey("down")){
transform.Translate(Vector3(0,0,-1) * playerSpeed * Time.deltaTime);
}
if (Input.GetKey("right") && Input.GetKey("up")){
fireballScript.firingDirection = Vector3(-1,0,1);
}
//this is the one that is not working :-(
if (Input.GetKey("left") && Input.GetKey("up")){
fireballScript.firingDirection = Vector3(-1,0,1);
}
if (Input.GetKey("left") && Input.GetKey("down")){
fireballScript.firingDirection = Vector3(-1,0,1);
}
if (Input.GetKey("right") && Input.GetKey("down")){
fireballScript.firingDirection = Vector3(1,0,1);
}
if(Input.GetKeyDown("space")){
Instantiate(fireBall, fireSpawn[fireDirection].position, fireSpawn[fireDirection].rotation);
}
Not in unity but in another program I had a similar problem, where it was testing if the keys were pressed but it was reading them at almost exactly the same time. I got around this by setting a boolean which was set if a key was pressed. i.e to 1 if the button was pressed and 0 if it was released. Therefore I was testing if both left$$anonymous$$eyPressed==1 && up$$anonymous$$eyPressed==1 then do something. This would only solve it if there were any issues if there is a problem with reading multiple keys in this case. Is it inside an update function? because as I understand it Get$$anonymous$$eyDown will reset at the next frame and wont be true again until the space bar is released and pressed again. Also, not a solution but in cases like this I have used switch ins$$anonymous$$d of multiple if's. Albeit probably not the answer I hope it helps a little. Cheers Chris
Answer by aldonaletto · Sep 01, 2011 at 04:18 AM
This is probably a limitation of your keyboard hardware. Keyboard keys form a matrix where each key connects a row to a column (8 rows and 16 columns, for instance). This arrangement allows detection of any two keys at the same time, but three or more keys may not be detected, depending on the rows and columns they connect. If three keys connect R1C1, R1C2 and R2C2, for instance, the circuit can't identify all of them because they short circuit all four lines when pressed at the same time.
I found your answer quite fascinating. I'm having the same exact issue. As a test, I opened Notepad and tried the same key combinations: UP+RIGHT+SPACE, DOWN+RIGHT+SPACE, UP+LEFT+SPACE (all creates a space in the document), DOWN+LEFT+SPACE does not. This seems like a huge limitation in the hardware. How have I not noticed this before? So there's no work-around for something like this? If I played Zelda or something in an E$$anonymous$$U I would have the same problems? Thanks for the detailed response. PS, I would give you an upvote but I need 15 rep. Great answer.
One more afterthought: I tried with the WASD keys and didn't have any problem at all. I like to give my users the choice of WASD or arrow keys, but it's so unfortunate that (at least with certain keyboards) users of the arrow keys will be at a disadvantage.
Unfortunately, there's no workaround for this issue. Even worse, keyboards from different manufacturers may have different row/column assignments, thus the combination that works in a given keyboard may not work in another one. The keyboard designers usually avoid problems with the most frequent combinations like Ctrl-Alt-Del (PC), but there will always exist lots of "forbidden" combos with a matrix keyboard. The only way to avoid forbidden combos would be to have a keyboard chip with one input for each key, but this would be too expensive - specially nowadays, when everything is made in China with the lowest cost possible!
Answer by Pbelling78 · Sep 01, 2011 at 04:44 AM
thank you aldonaletto, i kind of had a suspicion that it might be because of the keyboard but obviously wasn't for sure...i had no idea thats how a key board worked :-D
Your answer
![](https://koobas.hobune.stream/wayback/20220612195440im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to create a light effect on weapon fire 0 Answers
Gun Turret Script. Help with distance C# Script 1 Answer
Hold Spacebar, Stop Jumping 1 Answer
Update() Function only fires once. 1 Answer