0 votes

I want to create a laser, but the end of the line is magnetized to a certain point

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
        var colPoint := get_collision_point()
        print(colPoint)
        $Line2D.points[1] = to_local(colPoint)
    else:
        enabled = false
        $Line2D.points[0] = Vector2.ZERO
        $Line2D.points[1] = Vector2.ZERO

preview: https://drive.google.com/file/d/19LV5phdF3h7b4CGzePVmBgppk1x-wrY1/view?usp=sharing

Godot version 3.2.3.stable.official
in Engine by (118 points)
edited by

1 Answer

0 votes

If you look at the docs for get_collision_point() it says

Returns the collision point at which the ray intersects the closest object.

So since the edge of the cross is the closest object, the end of the line is stuck in it. You'll probably just have to use another method.

by (6,298 points)

I already understood that this is due to the length of the raycast

So what do you want to do then?

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.