Because of your else block, if A is not pressed both right and left are set to false in the animator, despite D potentially being pressed. I assume you'd want both if statements to have their own else to set the bool to false
would it? The code is using GetKeyDown, not GetKey. (but it's well past midnight here and I was planning on going to sleep hours ago so I'm not going to trust my logical thinking even that far at the moment :D)
It's still good practice to split left and right into their own logical states, and if you hit both A and D on the same frame it'd skip the A input. There's other issues with this at a lower level (like if this is in Update, it's unnecessarily setting left/right anim bools false almost every frame) but honestly for someone's early programming adventures, yeah just replacing the middle with an else if will work.
yeah, and since the logic on the code side is basically a trigger, it would make more sense to just use animator triggers rather than a bool. No need to reset it manually, and clearly the same logic and intention on both code and animator side of things.
35
u/Costed14 Oct 10 '26
Because of your else block, if A is not pressed both right and left are set to false in the animator, despite D potentially being pressed. I assume you'd want both if statements to have their own else to set the bool to false
105
u/ihufa Oct 10 '26
I thought it was something political at first lol
7
u/ProactiveCactus Oct 10 '26
Thought this was a political post at first ngl
5
u/Nearby-Pizza-8823 Oct 10 '26
Your code in the else block is only happening if A isn't being held down. It isn't checking for the D key.
4
u/xepherys Oct 10 '26
An 'else' always hangs off the previous 'if', so you basically have two distinct statements here:
So if you press D it get set true for about a microsecond before right and left are both set false.
The easiest way to fix this would be:
if (D) right
else if (A) left
else both false
There are better patterns, but understanding the basics first is more important.
2
u/HouseOfWyrd Oct 10 '26
Correct me if I'm wrong but the else statement is only tied to the second if statement.
Switch the second if to an elif and this should work.
1
u/Tangyhyperspace Oct 10 '26
OHHHH ITS KEYS
0
u/Kondor0 @AutarcaDev Oct 10 '26
Don't use an else, that will activate in every frame that that key is not pressed and it will overwrite the first if.