Finding mouse position relative to player

Godot Version

4.5.1

Question

I’m making a basic 2d top down shooter, and Godot is my first ever foray into game dev – I’m also actively learning how to code at the same time, so lots of learning happening. I also am trying not to use AI at any point in my dev process, since I want to actively learn how to code.

Right now, my code for determining how the player aims is:

var mouse_position = get_viewport().get_mouse_position() look_at(mouse_position)

Problem is, I want the playable area to be larger than the viewport. When I attach a Camera2D to the player, movement works as intended, but the aiming position of the mouse becomes all wacky because it’s referencing the position in the scene as if the camera couldn’t move.

How would I go about making sure the player always rotates to face where the mouse is the way you’d expect a top down shooter to?

Use get_local_mouse_position() instead.

1 Like