Godot Version
Godot Engine v4.4.dev7.official
Question
I have a GridMap in a 3D space with a Camera overlooking the scene. The goal is to click on a grid cell (using RayCast3D) and place a box there. This part works perfectly.
The problem arises with the removal functionality: it only works when I click on a box that is positioned along the edges of the GridMap. When I try to click on the top of a box that’s not at an edge, the removal doesn’t work. It seems like the RayCast3D isn’t detecting which grid cell I’m clicking on when interacting with the top of the box, so the removal action fails.
This is the code I am using for the ray picker camera:
extends Camera3D
@export var gridmap: GridMap
@onready var ray_cast_3d: RayCast3D = %RayCast3D
func _process(delta: float) -> void:
var mouse_position: Vector2 = get_viewport().get_mouse_position()
ray_cast_3d.target_position = project_local_ray_normal(mouse_position) * 1000.0
ray_cast_3d.force_raycast_update()
if ray_cast_3d.is_colliding():
var collider = ray_cast_3d.get_collider()
if collider is GridMap:
var collision_point = ray_cast_3d.get_collision_point()
var cell = gridmap.local_to_map(collision_point) #detects the cell the mouse is hovering over
var current_item = gridmap.get_cell_item(cell)
#Replace Floor[1] with Wall[0]
if current_item == 1:
if Input.is_action_pressed("place_item"):
gridmap.set_cell_item(cell, 0)
#Replace Wall[0] with Floor[1]
if current_item == 0:
if Input.is_action_pressed("remove_item"):
gridmap.set_cell_item(cell, -1)
gridmap.set_cell_item(cell, 1)
Below is an example:
Red square: Areas where if i click remove, it doesn’t remove the box (wall).
Green square: Areas where if i click remove, it removes the box (wall).