+1 vote

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:

1) How do I transform the coords of global mouse click captured by the node into coordinates inside the node?

2) 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?


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

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)
asked Dec 25, 2017 in Engine by Antonb (61 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.