This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+2 votes

Hi I am trying to use a line2D to draw a line between two sprites for a second to indicate a laser being fired. My problem is that the "lasers" show up positioned wrong. The target in the example below is a KinematicBody2D (same as the parent of this Line2D node. Its the same scene). I have some other logic around setting destinations for these ships and selecting them that uses global_position too and it works fine.

I think I'm stuck because I'm still getting the hang of thinking in terms of these different coordinate systems.

extends Line2D

var damage = 0
var laser_time = 1

var timer = null
var gun_target = null

func _ready():
    timer = get_node("gun_timer")

func _physics_process(delta):
    if timer.is_stopped():
        gun_target = null

func shoot(target):
    gun_target = target

game screenshot

in Engine by (23 points)

2 Answers

+1 vote
Best answer

Looks like the solution was to call to_local on both Vector2 before adding the points. This, sort of, makes sense to me. Probably need to review the docs about the coordinate system etc.

by (23 points)
edited by
0 votes

I do something similar, I have a simple "chain lightning effect that makes a line move between objects. This is the draw method (3.0). I move the object towards the object. This draws a line from where the object is to the other object.

func _draw():
    var from = _origin_pos - get_global_position()
    var to = Vector2(0, 0)

    from = from.normalized() * 100
    to = to.normalized() * 100

    # blue
    draw_line(from, to , Color(0, 0, 1), 5)
    # white
    draw_line(from, to, Color(1, 1, 1), 1)

And this is how I move the object toward its destination. I call this from _process() but should be fine in _process_physics...actually maybe that's where it SHOULD be.

func _move_towards_target(delta):
  # _wr_target_obj is a weak reference to the object I'm trying to 
  # move towards.
    var target_obj = _wr_target_obj.get_ref()
    var _target_pos = target_obj.get_global_position()
    var pos = get_global_position()

    var dist_to = get_global_position().distance_to(_target_pos)

    var movement = _target_pos - pos
    movement = movement.normalized()
    movement = movement * delta *  CHAIN_SPEED
by (141 points)

I'm glad you posted this because making sure I don't keep around refs to nodes is one thing I have been meaning to fix. Didn't know about get_ref. I don't think this solves my root problem though - that the coordinates I am using for my line are wrong. If I were to follow this I would still be using the same positions. I am using global_position but It doesn't seem like that is working. It does work for the movement of ships ( they move to where I click on the screen. Even when the camera is scrolled.)

I suspect I am not accounting for how the camera affects positions. Or it could be something else.

Have you tried tacking on the normalized()? I remember knowing how this all worked when I made that method. It has since left me.

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.