+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)
in Engine by (63 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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.