Godot Version
Godot 4
Question
Hi I’m new to Godot. Currently I am trying to program my player’s movements through mouse operations. I got the character to follow my mouse and it works well. However, I am having some trouble programming the animations to correspond the direction of the mouse. Does Godot have any features (through coordinates, collision objects, etc.) that can help detect which direction the mouse is moving? Thanks
This may help find the direction of the mouse from an object like the player:
direction = (get_global_mouse_position()-global_position).normalized()
you can put it in the process function of your players script to detect the direction of mouse from the player , just declare the variable direction above
If I understand the problem scope correctly you could use direction’s angle to index an animation.
If you have 8 animations, one for each direction and in-between, then you could create an index like so
var animation_index: int = roundi(direction.angle() * 8 / TAU)
This will equal a value between 0 and 8, where 0 is facing right, and continuing clockwise 2 would face down, 4 left, and 6 up.
You can use this with an array of your animation names in that same order.
const ANIMATION_DIRECTIONS = ["walk_right", "walk_right_down", "walk_down", etc ...]
$AnimatedSprite2D.play(ANIMATION_DIRECTIONS[animation_index])
1 Like