+1 vote

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

in Engine by (174 points)

1 Answer

+1 vote

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.

by (449 points)
edited by
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 Frequently asked questions and 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 [email protected] with your username.