Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Antonb | |
Old Version | Published before Godot 3 was released. |
Hi,
I have a sub-node (2d) in my game which should capture the mouse click coordinates and move the player sprite to them. Basically, it’s a map which displays above the game window.
I have 2 questions:
-
How do I transform the coords of global mouse click captured by the node into coordinates inside the node?
-
How can I avoid capturing clicks outside of the map node without having to attach things like Area2D to it? Or is it the only way?
Thanks!
Here’s the code:
extends Node2D
# class member variables go here, for example:
# var a = 2
# var b = "textvar"
var begin = Vector2()
var end = Vector2()
var path = []
const WALK_SPEED = 100
func _ready():
# Called every time the node is added to the scene.
# Initialization here
set_process_input(true)
func _input(event):
if (event.type == InputEvent.MOUSE_BUTTON and event.pressed and event.button_index == 1):
print("Click position: ", event.pos)
begin = get_node("Panel/Node2D/Player").get_global_pos()
print("Begin: ", begin)
# Mouse to local navigation coordinates
end = event.pos - get_mouse_pos()
print("End: ", end)
get_node("Panel/Node2D/Player").move_to(end)