3D: How to get gridmap tile using intersect_ray?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Macryc

Hi All and happy Easter!

I am using intersect_ray to capture collision points with my grid map (for point and click movement).
I want to detect gridmap tiles underneath the mouse. I’ve found a method called get_cell_item but cannot get it to work. hovered_point is returned by the intersect_ray method (i’m using position). I get invalid get index ‘position’ on base ‘Dictionary’. I’m guessing this is referring to ‘position’ being a field in the intersect_ray dictionary and I am able to reference it in other parts of the code, but not here… How does get_cell_item really work?

The code:

func _physics_process(delta):
	var hovered_point = (raycast from mouse collision point).position
	var hovered_tile = gridmap.get_cell_item(hovered_point.x, hovered_point.y, hovered_point.z)
:bust_in_silhouette: Reply From: Macryc

Ok i think i’ve found what was wrong.
Intersect ray returns a 3d vector position alright, but the grid map makes no sense of it until that position is mapped onto the grid map itself. I found this method world_to_map and is seems to have done the trick:

var hovered_point = gridmap.world_to_map(raycast from mouse collision point).position)
1 Like