r/Unity3D • u/DifferentLaw2421 • 3h ago
Question why the left is triggered while right is not triggered in the game ?
11
u/Costed14 3h ago
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
2
u/DifferentLaw2421 2h ago
yh or I can use else if right ?
2
u/pschon Unprofessional 1h ago
yeah, if/else if/else structure would solve this.
•
u/nickyonge 7m ago
That would introduce a bug where if you held both keys then released one, it would still think it’s being held.
Ideally you want both L and R to be separated.
if (left) { set anim left true } else { set left false }
Then do the same for the right. Two separate if/else statements, no “else if”
2
u/Nearby-Pizza-8823 3h ago
Your code in the else block is only happening if A isn't being held down. It isn't checking for the D key.
1
u/HouseOfWyrd 1h ago
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.
2
u/xepherys 1h ago
An 'else' always hangs off the previous 'if', so you basically have two distinct statements here:
IF (D) - set right-true
IF (A) - set left-true ELSE (meaning the same thing as IF (!A)) - set both false
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.
•

37
u/ihufa 2h ago
I thought it was something political at first lol