Skip to content

Commit 5aac61c

Browse files
Add documentation on flipping for diagonal movement
Added instructions for handling diagonal movement animations and correcting animation flips based on player input. This fixes "moving sideways and down causes player to flip back on its feet" animation state
1 parent 0380fe5 commit 5aac61c

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

getting_started/first_2d_game/03.coding_the_player.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,53 @@ movement. Let's place this code at the end of the ``_process()`` function:
323323
Play the scene again and check that the animations are correct in each of the
324324
directions.
325325

326+
327+
You may notice that the animations for moving diagonally are not yet all aligned
328+
with the animation for moving straight down. As a result, the player may appear to flip
329+
incorrectly or play the wrong animation when switching from moving down to diagonally down.
330+
331+
To correct this, we also need to check for cases where multiple movement keys are pressed
332+
at the same time. Based on the direction the player is moving, we can then adjust the
333+
animation’s flip settings using the same boolean checks we used earlier. This ensures the
334+
correct animation is displayed no matter which combination of inputs the player uses.
335+
336+
.. tabs::
337+
.. code-tab:: gdscript GDScript
338+
339+
if(velocity.x != 0 && velocity.y !=0): # If vertical and horizontal movement keys are pressed
340+
$AnimatedSprite2D.animation = "walk"
341+
$AnimatedSprite2D.flip_h = velocity.x < 0
342+
$AnimatedSprite2D.flip_v = velocity.y > 0
343+
elif velocity.x != 0:
344+
$AnimatedSprite2D.animation = "walk"
345+
$AnimatedSprite2D.flip_v = false
346+
# See the note below about the following boolean assignment.
347+
$AnimatedSprite2D.flip_h = velocity.x < 0
348+
elif velocity.y != 0 :
349+
$AnimatedSprite2D.animation = "up"
350+
$AnimatedSprite2D.flip_v = velocity.y > 0
351+
352+
.. code-tab:: csharp
353+
354+
if (velocity.X != 0 && velocity.Y != 0)
355+
{
356+
animatedSprite2D.Animation = "walk";
357+
animatedSprite2D.FlipH = velocity.X < 0;
358+
animatedSprite2D.FlipV = velocity.Y > 0;
359+
}
360+
else if (velocity.X != 0)
361+
{
362+
animatedSprite2D.Animation = "walk";
363+
animatedSprite2D.FlipV = false;
364+
// See the note below about the following boolean assignment.
365+
animatedSprite2D.FlipH = velocity.X < 0;
366+
}
367+
else if (velocity.Y != 0)
368+
{
369+
animatedSprite2D.Animation = "up";
370+
animatedSprite2D.FlipV = velocity.Y > 0;
371+
}
372+
326373
.. tip:: A common mistake here is to type the names of the animations wrong. The
327374
animation names in the SpriteFrames panel must match what you type in
328375
the code. If you named the animation ``"Walk"``, you must also use a

0 commit comments

Comments
 (0)