How to detect grid cell based on mouse position | Godotv4.2.1

128 views Asked by At

A simple 3d node scene has a gridmap node and a camera with 45 degree rotation - looking at the grid. Now on running the scene I want to detect the cells according to mouse cursor position. Here is what I have tried it always returns blank. Please note that currently there are no meshes in the grid but I understand that the gridmap has its own collision layer irrespective of the fact wether the cells are painted or not.

func _physics_process(delta):
    var mouse_position = camera.get_viewport().get_mouse_position()
    var from = camera.project_ray_origin(mouse_position)
    
    var to = from + camera.project_ray_normal(mouse_position) * camera.far  
    var space_state := get_world_3d().direct_space_state
    var query := PhysicsRayQueryParameters3D.create(from, to)
    var result := space_state.intersect_ray(query)      
    print(result)
    print("=================")
    

Tried the above code under _input function too.

result is always empty dictionary. I m new to godot and still trying to figure out raycasting and 3d to 2d projection. Using Godot version 4.2.1.

0

There are 0 answers