0 votes

I'm fairly new to game programming. I tried replicating this effect from Polymar's video (https://www.youtube.com/watch?v=iEn0ozP-jxc&t=136s, 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)
    pass

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

Godot version 3.2.2
in Engine by (15 points)

1 Answer

+1 vote
Best answer

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)

https://docs.godotengine.org/en/stable/classes/class_vector2.html

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)
by (2,058 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.