How do I get direction from mouse position?

Godot Version

4.4.1

Question

How do I get a direction from the relation between the player character and the mouse position, without the character looking at the mouse? The goal is when I click the screen a projectile goes towards the mouse position.

Hi,

Mouse position can be retrieved using get_global_mouse_position().
To get the direction to the mouse, you can do something like:

var direction := global_position.direction_to(get_global_mouse_position())

That will just get a direction vector but not affect anything else (your player will not look at it as there’s no code doing that).
Note that I’m using global_position which will use the position of the node the script is on, but you can use a Vector2 of any source.

Then, applied that direction variable to anything you want. In your case, that would be your projectile.

1 Like

I didn’t think about this, but how to I get the projectile’s rotation to face the mouse when I fire it off?

you can use rotation = direction.angle()

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.