Godot Engine

Godot Engine

Quenkslet Mar 20, 2018 @ 9:24pm
How to get 3D coordinates from mouse cursor
How to make a script that gets a Vector3 from the mouse cursor, assuming the z axis is fixed (just like gridmap coordinates).
< >
Showing 1-4 of 4 comments
phillippi2 Mar 20, 2018 @ 11:45pm 
I once did it by casting a ray from the cursors' screen coordinates, and getting the X,Y and Z coordinates of where that ray collided with the geometry
Quenkslet Mar 21, 2018 @ 12:05am 
Originally posted by phillippi2:
I once did it by casting a ray from the cursors' screen coordinates, and getting the X,Y and Z coordinates of where that ray collided with the geometry
Any examples?
Camera has 2 methods named: project_ray_origin and project_ray_normal, both taking in a Vector2 which you can easily get with a get_viewport().get_mouse_position()

(since godot display everything through viewports, it allow you to use the same code be it in split screen or a in-game display like in-game -àla Quake4/DOOM-)

But if you only cast rays, you won't get anything, so you need to get a variable with get_world().direct_space_state to directly adress the physics server, then you can use the method intersect_ray() of the physics server, intersect_ray need 2 Vector3, one the origin, another one the direction.

intersect_ray will then return a dictionnary, which contain things like RID, collider's ID, collider and such, search the doc under PhysicsDirectStateSpace for more info on the method.

LPT: multiplying project_ray_normal by at least the value of your camera-mesh distance is important, otherwise, you won't hit anything.
Quenkslet Apr 27, 2018 @ 4:59am 
I've solved it.

Steps:
  1. Put an static body area on the place you want to get the coordinates.
  2. Attach a input_event signal to a script.
  3. print(click_position)

https://preview.ibb.co/j2r8pH/Screenshot_from_2018_04_27_18_57_11.png

I can use it to knock down objects

https://image.ibb.co/nCWxic/Screenshot_from_2018_04_27_18_57_14.png

It tracks my mouse location excactly.

https://image.ibb.co/kTjuUH/Screenshot_from_2018_04_27_18_57_22.png

Even when the camera is moving.
Last edited by Quenkslet; Apr 27, 2018 @ 5:15am
< >
Showing 1-4 of 4 comments
Per page: 1530 50

Date Posted: Mar 20, 2018 @ 9:24pm
Posts: 4