Is it possible to know if object B is colliding with Raycast generated by object A?

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

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 "_on_Area_entered" for Object B, only relative to Raycasts?

ps: i even made a drawing to help explain

:bust_in_silhouette: Reply From: denxi

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.