I’m working on a top-down shooter and I used regular key input for aiming, but found that it was a little awkward to use, so I want to use the mouse instead. Problem is, I don’t know how to do it. I know how to define the mouse variable, but that’s all I know. Can someone please provide me with the method I should use to make the player rotate relative to the mouse’s position on the screen? The rotation has to be restricted to the z-axis.
.blend will be available if anyone needs it, although I don’t see why it would be needed.
Thanks in advance!
I did a tutorial for Riyuzakisan on how to derive 3D coordinates for the current mouse position: Mouse position in 3D space.
So, that may help.
Also, in my “Tunnel Runner” series, I use the mouse to control the player, so that could give you some relevant information.
If you have a mouse cursor standing in in an overlay scene, I would get the mouse cursor’s screen position by the overlay scene camera’s getScreenPosition function, and do the same for the player. Then, get the angle between them with math.atan2(cursorpos.y - playerpos.y, cursorpos.x - playerpos.x), and rotate the player to that angle with a Euler variable (you can either convert the Player’s orientation to a Euler variable with player.orientation.to_euler(), or you can do so with a simple Euler() variable).
If you want, I could make a tutorial.
EDIT: Although, Goran’s method’s easier since you wouldn’t have to get the cursor’s coordinates; you could just use the mouse’s directly.
Thanks a lot guys! Goran your tutorial is just what I’m looking for.:yes: Thanks man!