Rotation of a node based on the mouse location

Godot Version

Godot 4.2

Question

Good day to everyone! Can you help me with my problem? The thing is I have a player node, and inside that is a weapon. Now, when I input an event, (left click button) the weapon will fire an arrow. When the arrow is instantiated, what I want is it will rotate on where the weapon is rotated, how can I do that?

Here are a few options:

Option 1 - Using the mouse position with look_at

var mousePos = get_global_mouse_position()
arrow.look_at(mousePos)

Option 2 - Using the arrow’s velocity with look_at

var arrowSpeed = 10;  // Set this value to whatever you want

var mousePos = get_global_mouse_position()
var arrowDirection  = (mousePos - global_position).normalized()
var arrowVelocity = arrowDirection * arrowSpeed

arrow.look_at(arrow.global_position + arrowVelocity)

Alternatively, consult the Godot Docs’ 2D movement overview page.

Oh, I forgot to mention, after it’s intantiated, I don’t want it to follow my mouse anymore.

Just because you compute the orientation/rotation of the arrow based on the mouse position doesn’t mean it will follow the mouse.

If you would actually want to make the arrow follow the mouse, you need to perform a continuous update in _processing() or similar.

Have you clicked on the link I added in my last reply?