Godot Version
4.4.dev7.official
Question
I have a GridMap, with a MeshLibrary with three items (0: floor, 1: wall, 2: glass_wall [just meshes/boxes]]). Let’s call it blocks.
I attempted a conversion to get the location of a block and then remove it when I click R [remove_item]. Right now, it only removes the block if i click slightly to the edge of a block, but if the Camera is looked straight down where no edges are visible then nothing is removed.
I am using RayCamera3D with top-down view and a GridMap.
RayCamera code:
extends Camera3D
@onready var ray_picker_camera: Camera3D = $"."
@onready var ray_cast_3d: RayCast3D = %RayCast3D
@onready var hot_bar: ItemList = $HotBar
var mouse_position
var selected = 1
func _process(_delta: float) -> void:
mouse_position = get_viewport().get_mouse_position()
#remove item is click R and palce item is click E
func _physics_process(_delta) -> void:
if Input.is_action_pressed("remove_item"):
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():
if ray_cast_3d.get_collider().has_method("destroy_block"):
ray_cast_3d.get_collider().destroy_block(ray_cast_3d.get_collision_point() - ray_cast_3d.get_collision_normal())
if Input.is_action_pressed("place_item"):
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():
if ray_cast_3d.get_collider().has_method("place_block"):
ray_cast_3d.get_collider().place_block(ray_cast_3d.get_collision_point() + ray_cast_3d.get_collision_normal(), selected)
#CURRENT ERROR: Items not being removed properly.
#handle block selection
if Input.is_action_just_pressed("Number1"): #grey walls
selected = 1
hot_bar.select(0)
if Input.is_action_just_pressed("Number2"): #glass walls
selected = 2
hot_bar.select(1)
and the GridMap code is the following:
extends GridMap
func destroy_block(world_coordinate):
var map_coordinate = local_to_map(world_coordinate)
set_cell_item(map_coordinate, -1)
func place_block(world_coordinate, item_index):
var map_coordinate = local_to_map(world_coordinate)
if get_cell_item(map_coordinate) == 0:
set_cell_item(map_coordinate, item_index)
The code worked perfectly the other day. I added a hotbar today and for some reason something went wrong, I just don’t know where exactly. Camera3D has a RayCast3D node and an item list [hotbar]. Gridmap has no nodes, and the mesh library is selected.