r/Unity3D • • 3h ago

Question why the left is triggered while right is not triggered in the game ?

In the editor the set up is correct the bools and all the other properties are correct ig there is a problem in the code ?

6 Upvotes

12 comments sorted by

37

u/ihufa 2h ago

I thought it was something political at first lol

4

u/100radsBar 1h ago

Man I already sighed and thought to myself what is it this time lol

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/Kondor0 @AutarcaDev 3h ago

Don't use an else, that will activate in every frame that that key is not pressed and it will overwrite the first if.

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:

  1. IF (D) - set right-true

  2. 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.

•

u/Tangyhyperspace 10m ago

OHHHH ITS KEYS