How do I get an angle based on my mouse location?

Hello,

I am creating a 2-D side scroller. I want to be able to shoot in different directions based on where the mouse is. How do I calculate an angle from the player’s location and the mouse’s location? I was thinking something along the lines of Make Rot from XZ (I’m using these 2 axes). I’m not really sure how to set this up.Any help would be appreciate.

Thanks.

To get the angle of a vector(x,y), you can just apply atan2(y,x).

Here’s what it could look like if the player is at the center of the screen:

If your player is not at the center of the screen, instead of


mousePositionOnViewport/viewportSize-(0.5,0.5)

you could do


mousePositionOnViewport-PlayerPositionOnViewport

2 Likes