[Suggestion] Character can only face right when moving up or down

Teacher’s code is as follows:

direction.x < 0.f ? rightLeft = -1.f : rightLeft = 1.f;

However this makes the character always face right when you’re moving only up or down.
To fix it I used:

if (direction.x < 0.f) {rightLeft = -1.f;}
else if( direction.x > 0.f) {rightLeft = 1.f;}

A minor incovenience but small details can make a big difference.

2 Likes

Hello, i see your suggestion and I want to ask if you can explain me de difference between Teacher’s code and your’s.
Sorry if my english it’s not good, it’s not my native language.

2 Likes

Hello, I’ll try to explain. Don’t worry about your English I’m not native either :slight_smile:

Ternary statements don’t operate the same way an if - else if statement operates.

When you’re moving just up or down, your direction.x will be equal = 0.
Try to imagine, what happens in each version when direction.x = 0?

Teacher’s code forces the character to face right when it is moving just up or down. It can never move up or down looking to the left.

My version keeps rightLeft unchanged when direction.x = 0 so the character will keep looking to the direction it was looking at.

If direction.x = 0 then direction.x < 0 = false. (Equal to zero isn’t less than zero)
If direction.x is equal to zero, then direction.x isn’t less than zero.

If you have any more questions, I’ll be glad to help.

1 Like

thank you so much for your response, your time and sorry for the delay.
I realise that the error of the teacher was that else it would be including 0, or something like that, I realise that days ago and forget to response jejeje.
In any case thank you so much :3

1 Like

Privacy & Terms