This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

[Godot 2.1.4] Ray cast from camera not working

woopdeedoowoopdeedoo Posts: 109Member
edited November 2018 in 3D

I'm trying to cast a ray from the camera into 3D space, and I followed the examples from the docs, but it's not working for some reason. Shouldn't this work?

var ray_query_from
var ray_query_to
var mouse_pos
#
# (...)
#
func _input(event):
    if event.is_action_pressed("editor_select"):
        mouse_pos = get_viewport().get_mouse_pos()
        ray_query_from = camera.project_ray_origin(mouse_pos)
        ray_query_to = ray_query_from + camera.project_ray_normal(mouse_pos) * 1000
        set_fixed_process(true)
#
func _fixed_process(delta):
    var space_state = get_world().get_direct_space_state()
    var result = space_state.intersect_ray(ray_query_from, ray_query_to, [self])
    print("result: ", result)    #<--------   always comes out empty
    set_fixed_process(false)

I have several Areas with CollisionShapes set with BoxShapes that should be being hit but aren't. result dict is always empty.

Best Answer

  • woopdeedoowoopdeedoo Posts: 109
    edited November 2018 Accepted Answer

    Nevermind. I double checked the intersect_ray() function and by default it's not set to collide specifically with Areas.
    Fixed by changing line 16 to:

    var result = space_state.intersect_ray(ray_query_from, ray_query_to, [self], 0xFFFFFF, space_state.TYPE_MASK_AREA)
    

Answers

  • woopdeedoowoopdeedoo Posts: 109Member
    edited November 2018 Accepted Answer

    Nevermind. I double checked the intersect_ray() function and by default it's not set to collide specifically with Areas.
    Fixed by changing line 16 to:

    var result = space_state.intersect_ray(ray_query_from, ray_query_to, [self], 0xFFFFFF, space_state.TYPE_MASK_AREA)
    

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.