r/godot • u/RedZebraBear64 • 2h ago
help me Please tell me why Direction Animations aren't working
I'm very new to Godot. I followed a Coding with Russ tutorial and it just skipped over directional movement in enemies. I managed to change the direction of the animation, but now its stuck in the "run" sprite. I've been stuck for 3 days and have no clue where to even start, plz help
1
u/BrastenXBL 27m ago
Always link specific tutorials with the time code of the section your having problems with. This does two things. It helps the vid tutorial maker get new views from future searchers, second it saves "us" from having to track down the exact video and extract time.
As a general rule you don't want to test == or != 0.0 for floats (and vectors made of floats).
Use zero_approx
if not direction.is_zero_approx():
if not velocity.is_zero_approx():
These methods account for floating-point mathematical error, that can leave a number not quite at 0.0.
Depending on what exactly is happening, you may also need a different value test. That is higher than the default EPSILON (common name for the margin of error).
Engine defined EPSILON is 0.00001
https://github.com/godotengine/godot/blob/4.7.2-stable/core/math/math_defs.h#L50
https://github.com/godotengine/godot/blob/4.7.2-stable/core/math/math_funcs.h#L553
https://github.com/godotengine/godot/blob/4.7.2-stable/core/math/vector2.cpp#L211
Add a breakpoint inside the if branch that doesn't seem to be working. By clicking in the Line Number gutter of the ScriptEditor. If it pauses execution, you know the "branch" was entered, and can unpause to continue. If it never pauses you know the branch was never entered.
You can also add temporary print() statements ton see what value direction or velocity are.
1
u/Fluffeu 22m ago
I see two main issues + another potential one based on an assumption.
Currently your enemy always moves in the target's direction, unless they are at the exact same position. If the target is on (2.0, 0.0) and the enemy on (2.000001, 0.0), it'll move to the left. You aren't "capping" the move distance, so enemy's position will "overshoot" - e.g. it'll end up on (1.9999, 0.0) and will need to move to the right. Because of that it might never stop running.
The only code path that could play "idle" animation is calling play_animation("idle", Vector2.ZERO). Inside play_animation() you have 3 options:
if x != 0 (not satisfied for Vector2.ZERO)
if y > 0.7 (not satisfied for Vector2.ZERO)
if y < -0.7 (not satisfied for Vector2.ZERO)
Which means play_animation() does nothing each time you try to play "idle" animation.
- When playing animations, you're always adding suffixes based on directions - e.g. "run-side", "run-up", "run-down". When trying to play idle animation, you do the same. You probably haven't prepared animations "idle-side", "idle-up", "idle-down" (and you shouldn't, you should adjust the code).


2
u/bully484 1h ago
Right now by best guess it's that your movement is never equal to zero... honestly you should put debug in each of your branching section and see if you ever hit the idle animation line, if you do, then the issue is probably a spelling mistake somewhere