How to properly rotate a 2D sprite away from the mouse cursor?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Napstaguy04

I’m fairly new to game programming. I tried replicating this effect from Polymar’s video (, around 4:31) where he makes his arrow sprite turn away from the mouse by using a trigonometric function.

However, when I tried replicating the code in godot, the sprite just ends up rotating in a different way than intended, sometimes sporadically turning and slowing down in random angles. Here’s the code that I’ve written:

extends Sprite

func _process(delta):
	rotation = atan2(position.y, get_global_mouse_position().x)

Are there extra parameters that I need to insert? Or have I made the code wrong? Help is appreciated, thanks!

:bust_in_silhouette: Reply From: estebanmolca

I tried to use your example with atan and the local mouse position but it seems that in each frame it does not work well. However it works like this:

func _process(delta):
	rotation = get_global_mouse_position().angle_to_point(position)

Later if you want to correct the rotation, for example if the arrow does not point where you want, you can add or subtract a constant angle:

func _process(delta):
	rotation = get_global_mouse_position().angle_to_point(position)
	rotation += deg2rad(180)