How can I find out where a StaticBody was clicked when in Input.MOUSE_MODE_CAPTURED?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By louisVottero
:warning: Old Version Published before Godot 3 was released.

In 3D I’m trying to click on a StaticBody and using _input_event.

With

Input.MOUSE_MODE_VISIBLE

I can click on the StaticBody and create something where the click happened.

With

Input.MOUSE_MODE_CAPTURED

I get no mouse events in the StaticBody for _input or _input_event.

How can I find out where a StaticBody was clicked when in Input.MOUSE_MODE_CAPTURED?

:bust_in_silhouette: Reply From: avencherus

Check to make sure you have a suitably sized collision shape added to it, and that the StaticBody has the option Ray Pickable turned on.

After that click events from the viewport should cast rays that are received and reported to the StaticBody’s _input_event(), the received parameters will tell you everything you need to know about the collision.

func _input_event(camera, event, click_pos, click_normal, shape_idx):

Which works has long I have:

Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

However as soon as I set it to:

Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

Input invents on my StaticBody no longer are triggered when the mouse clicks/moves

I will experiment some more but haven’t been able to get it to work.

louisVottero | 2016-11-05 14:57

Hmm, sorry, I’ve never tested such a thing.

It says it continues to track movement, maybe it’s not a compatible thing.

If you’re able to track to the motion and clicks with regular input events, then you can cast your own rays.

The camera can give you a unprojected ray normal, based on the viewport coordinates.

You can put a RayCast node on the camera and call something like this, and then test the ray intersection in the fixed_process.

my_ray.set_cast_to(my_camera.project_ray_normal(event.pos) * ray_length)

Or you can also store the coordinates, and during the fixed_process grab the world state and use the PhysicsServer to cast a ray.

func _fixed_process(delta):
	
	var state = get_world().get_direct_space_state()
	var intersections = state.intersect_ray(from_vector, to_vector)

avencherus | 2016-11-05 16:39

Yep, so _input started working. Not sure what I changed that changed that. Maybe I was just reading it wrong. With _input reading mouse clicks I am able to read a ray parented under the player camera. I can get what it intersects with as well as the position where it intersects. _input_event is now useless but I’ve been able to recreate the behavior.

louisVottero | 2016-11-05 23:25