0 votes

So, I have an object A with a Raycast in it; this Raycast must necessarily be in object A; the point is: this Raycast of A will at some point collide with object B; to get the information in A's Script about the collision of Raycast A is simple; however, is there any way I can get this Raycast collision from A to also inform object B that it is colliding with this Raycast?

Any way like an "onArea_entered" for Object B, only relative to Raycasts?

ps: i even made a drawing to help explain

asked Jan 23 in Engine by lucasfazzi (161 points)

1 Answer

0 votes

You can just signal object B when the raycast detects that it's collided with it. Something like:

signal raycast_A_object_hit(object)

func _physics_process(delta):
    if $raycast.is_colliding():
        emit_signal("raycast_A_object_hit", $raycast.get_collider()) 

What that'll do is, when the raycast collides with any object, it'll pull a reference to that object and then emit a signal saying that it's hit something. That signal will also contain that object reference, so then you could have code in object_B that goes like:

func on_raycast_A_object_hit(object):
    if object == self:
        #do stuff

You'll have to connect the signal of course, either in the script or in the editor.

answered Jan 24 by denxi (403 points)
edited Jan 24 by denxi
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.