Get mouse position relative to a node

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By someantics

HI all!

I’m new to Godot so still figuring some things out.

I have a scene like this:
enter image description here
The camera is set to follow the sprite node. When I click on the game, I want the sprite to move to the position I have clicked on the tilemap but it always goes to somewhere different. What global/viewport/local conversion voodoo do I need to do to get the sprite to move to where I have clicked on the tilemap?


Can you show us the code you currently have for moving your sprite?
Are you moving it onto a cell on the tilemap or just somewhere in the world?

greencloversguy | 2019-08-11 08:08

:bust_in_silhouette: Reply From: johnygames

I have never used Navigation2D nor do I know what exactly it is that you wish to achieve here, but a simple way to just move your sprite to the desired coordinates is to simply instruct it to move to the global position of the mouse. Go to the project settings and under Input Map create a new mouse input, name it “mouse_click” and use the following code in the Sprite’s script:

func _process(delta):
	if Input.is_action_just_released("mouse_click"):
		position = get_global_mouse_position()

Unless there is any particular reason why this wouldn’t work with Navigation2D and Tilemaps or you want something more sophisticated, this will do.