Problem with character facing

hey,
when I press A for left and then press W for up or S for down, the knight facing right istead of left.
i know the problem is in this segment in the code but how do i fix it?
direction.x < 0.f ? rightLeft = -1.f : rightLeft = 1.f;

the whole segment is:
if (Vector2Length(direction) != 0.f)
{ /*Add to worldPos the result of scaling the
normalize “direction” vector by “speed”. */
worldPos = Vector2Add(worldPos, Vector2Scale(Vector2Normalize(direction), speed));
//check knight facing.
direction.x < 0.f ? rightLeft = -1.f : rightLeft = 1.f;
texture = run;
}
else
{
texture = idle;
}

When you press Up and Down (W or S), direction.x is 0. So the test in the expression direction.x < 0.f ? rightLeft = -1.f : rightLeft = 1.f; will always set rightLeft to 1.f as a value of 0 is not less than 0;

What you want to do, is check if direction.x != 0 first so that rightLeft stays at its previous value.

if(direction.x != 0){
    direction.x < 0.f ? rightLeft = -1.f : rightLeft = 1.f;
}
2 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms