0 votes

Hello,
I'm trying to make it so that a line aims at the mouse position while still rotating around a pivot that is not part of the line. To do so, I've made my own lookatmouse() function that calculates the angle between the x axis and the mouse, and then the angle between the line aiming at the mouse through the pivot and the line aiming at the mouse through the line. The sum of the two gives me the exact angle that makes it so that the line aims at my mouse. However, when moving the mouse somewhat close to the line's center, the angle starts going crazy and rotates back and forth for some reason.

I've got a MRE right here if you'd like to test it out: MRE

Godot version 3.2.3
in Engine by (12 points)

1 Answer

0 votes

Seems to work without the offset_angle added to rotation.

extends Sprite

func look_at_mouse():
    var mouse_pos = get_global_mouse_position()
    var mouse_angle = Vector2.RIGHT.angle_to(global_position - mouse_pos) # Angle between x axis and the line drawn from the pivot and the mouse_pos
    var offset_angle = (global_position - mouse_pos).angle_to($Line.global_position - mouse_pos) # The angle between (the line drawn from the pivot and the mouse_pos) and (the line drawn from the line center adn the mouse_pos)

    rotation = mouse_angle + offset_angle # Applying the sum of the two angles so that the line is the one actually aiming at the mouse and not the pivot

func _process(delta):
    look_at_mouse()
by (810 points)

I need the line to meet with the mouse though, the offset_angle is the one that corrects the trajectory and incidentally, is the one that causes problems.

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 Frequently asked questions and 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 [email protected] with your username.