0 votes

I've got a 2d top-down space game, visually similar to Space Rock, which y'all know very well

I have a randomly spawned enemies and a want to spawn them by two and have drawn a line between them. This line moves and changes it's length relatively to the positions of spawned NPC's and move with them together.
Also I've got a RayCast2D node, which should check if something collide with this line between NPC's. I tried to do this.
I added node RayCast2D as a child to node Line2D.
TargetLine.gd code:

func _process(delta: float) -> void:
if is_instance_valid(target1) and is_instance_valid(target2):
    points[0] = to_local(target1.global_position)
    points[1] = to_local(target2.global_position)

target1 and 2 are positions of the 2 NPC's, where the line has points. And when I try to set the position of a raycast and to set it's cast direction it just sets the position correctly(or maybe it's already set well because it's child node) but the cast direction is located somewhere far in space, and I have generally no idea, why. I can provide any further info, and... thanks in advance.

Godot version v3.3.4.stable.official.
in Engine by (60 points)

1 Answer

+2 votes
Best answer

Hi again :)
It seems that you set both of them to target2's position. But there's a second problem lurking. The cast position is in local space of the raycast itself. Hence you cannot use the parent's (Line2D's) to_local function. The solution is to use RayCast2D's to_local.

# Godot also allows to modify global_position, so no need to use to_local here
$Ray_collision_checker.global_position = target1.global_position
# Use ray's local transform here
by (382 points)
edited by

Dude, you helped me so much, twice. I hope everyone in need can find such a mentor))

I too do need help from time to time. So it's nice to be able to give back to the community whenever I have time and energy :)

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.