wrong line position

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Timofey

when in contact with the circle, the player is given a “laser” but it is not where it was planned
preview: https://drive.google.com/file/d/1dSwns7vVfKLwoVAxRrrpYsj0fnGkziQg/view?usp=sharing
circle:

extends Area2D

var p1: Object

func _ready() -> void:
    p1 = get_tree().get_nodes_in_group('p1')[0]

func _on_laserUpgrade_body_entered(body: Object) -> void:
    var laserBeam = load("res://upgrades/laserBeam.tscn").instance()
    laserBeam.position = p1.position
    p1.add_child(laserBeam)
    queue_free()

laser:

extends RayCast2D

func _ready() -> void:
    $Line2D.points[0] = Vector2.ZERO
    $Line2D.points[1] = Vector2.ZERO

func _physics_process(_delta: float) -> void:
    if Input.is_action_pressed("LMB"):
    	enabled = true
    	look_at(get_global_mouse_position())
    	$Line2D.points[0] = position
    	$Line2D.points[1] = to_local(get_collision_point())
    else:
    	enabled = false
    	$Line2D.points[0] = Vector2.ZERO
    	$Line2D.points[1] = Vector2.ZERO

There is no need to update the position of the laser when it’s made child to the circle; the initial positioning will suffice.
Either leave the laser’s starting position when it’s the child of the laser or do not make it child to the laser and update it’s position to match that of the circle.

So if you want to keep the laser as the child of the circle then maybe:
laser

func _physics_process(_delta: float) -> void:
    if Input.is_action_pressed("LMB"):
        enabled = true

        # If the parent is looking at the mouse
        # then so will the children. No need for this.
        #look_at(get_global_mouse_position())

        # Laser position is already set in the
        # _ready() method of the circle script and
        # _ready() of this script.
        #$Line2D.points[0] = position

        $Line2D.points[1] = to_local(get_collision_point())
    else:
        enabled = false

        # Again, this is already set.
        #$Line2D.points[0] = Vector2.ZERO

        $Line2D.points[1] = Vector2.ZERO

I didn’t set this as an answer since I haven’t tested it. Hopefully it is the answer.

Bengt Söderström | 2021-02-27 19:21

thank, this worked

laser:

extends RayCast2D

func _physics_process(_delta: float) -> void:
    if Input.is_action_pressed("LMB"):
    	enabled = true
    	look_at(get_global_mouse_position())
    	$Line2D.points[0] = position
	    $Line2D.points[1] = to_local(get_collision_point())
    else:
    	enabled = false
	    $Line2D.points[1] = Vector2.ZERO

circle:

extends Area2D

var p1: Object

func _ready() -> void:
    p1 = get_tree().get_nodes_in_group('p1')[0]

func _on_laserUpgrade_body_entered(body: Object) -> void:
    var laserBeam = load("res://upgrades/laserBeam.tscn").instance()
    p1.add_child(laserBeam)
	queue_free()

Can you tell me how you can attach a laser to the end of a character’s arrow when it touches the circle, and moreover, make the arrow rotate around the character’s body while watching the mouse?
character structure: https://drive.google.com/file/d/1GGUNpmZDvuqa831jW0o0DUj9NDeyDf0Z/view?usp=sharing

Timofey | 2021-02-27 19:52

For arrow placement:

  • Place your arrow sprite over the position you want it to rotate around.
  • Change the arrow sprite’s offset to be as far from the center as you’d like
  • Apply the look_at() code and change the above to get desired result

For arrow placement:

  • Add a test node as the child to the arrow node
  • Place the test child where you want the laser to start from
  • Note the position of the test node
  • Then when loading your laser in your code, place the laser at that previously noted position, as the child of the arrow.

I haven’t tested this.

Bengt Söderström | 2021-02-28 01:02