0 votes

I'm trying to cast a ray from object to current camera, but I never get any collision.

I have a Sprite3D object that has a RayCast child. It always returns false for is_colliding() method. What am I doing wrong?

extends Sprite3D

func _ready():
    set_process(true)

func _process(delta):
    var ray = get_node("RayCast")
    var camera_pos = get_tree().get_root().get_camera().get_global_transform().origin
    ray.set_cast_to(camera_pos)
    print(ray.is_colliding())
in Engine by (210 points)

1 Answer

+1 vote

Make sure to used fixedprocess instead of +process, otherwise the directspacestate() won't be accessible, and will return null, resulting in an error.

extends Sprite3D

func _ready():
    set_fixed_process(true)

func _fixed_process(delta):
    var camera_pos = get_tree().get_root().get_camera().get_global_transform().origin
    var halo_pos = get_global_transform().origin

    var ray = get_world().get_direct_space_state().intersect_ray(halo_pos, camera_pos)
    if(ray.empty()):
        show()
    else:
        hide()
by (210 points)
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.