Set animation based on mouse location

Godot Version

Godot 4


I want to set my player’s animation sprite up down or right (based from top down view) and have their animation change based on where they are looking. I don’t want to rotate the sprite, I just want to detect when the mouse is in a ‘dead zone’ to swap the animation to where the mouse is facing. This would work only in idle animations, but I am unsure of how to properly use get mouse input event

not everything you are looking for but a starting point. here is how to get:

  • distance from sprite to mouse
  • angle from sprite to mouse

keep in mind i am using the node’s global position and the mouse’s global position

extends Sprite2D

func _process(delta: float) -> void:
	var v_distance : float = global_position.distance_to(get_global_mouse_position())
	print("distance ", v_distance)
	var v_angle_in_rads : float = global_position.angle_to(get_global_mouse_position())
	print("angle in radians ", v_angle_in_rads)
	print("angle in degrees ", rad_to_deg(v_angle_in_rads))