0 votes

Hey!

To keep it short and sweet, I'm trying to draw a line from a Node2D to the Mouse Position. It's working decently, but for some reason, the line does actually touch the mouse or come anywhere near it for that matter. Here is what I mean:

https://i.gyazo.com/9b5e082cfc90d6766dbc015ec7bddc05.mp4
Example

Here is my script attached the Node2D placed directly on the orange square:

extends Node2D

func _physics_process(_delta):
    look_at(get_global_mouse_position())
    update()

func _draw():
    var color = Color(0, 1, 1)
    var from = self.position.normalized() * 100
    var to = get_global_mouse_position().normalized() * 100
    draw_line(from, to, color)

Thanks to anyone who can help me solve this problem!

Godot version 3.4
in Engine by (32 points)

1 Answer

+1 vote
Best answer

I think this should work assuming I am understanding your node structure correctly:

extends Node2D

func _physics_process(_delta):
    update()

func _draw():
    var color = Color(0, 1, 1)
    var from = Vector2.ZERO
    var to = get_local_mouse_position()
    draw_line(from, to, color)
by (3,768 points)
selected by

Perfect! Using the local mouse position fixes it perfectly, however, I don't understand why... I'm gonna do some research and see if I can figure out what caused this.

Thanks for your help!

No problem. The line is drawn relative to the node that script is attached to. So the origin point should just be the origin of the node, i.e. (0,0), then the end point should be where the mouse is in relation to the node, i.e. local coordinates.

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 Frequently asked questions and 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 [email protected] with your username.