Raycast-based block erasure not working properly

I’m trying to implement a block erasure system where right-clicking removes a block at the mouse position using a raycast, but it’s not working as expected. The raycast doesn’t seem to detect collisions properly.

In player script

func _unhandled_input(_event: InputEvent) → void:
if Input.is_action_just_pressed(“rmb”):
diggingRaycast.target_position = (get_local_mouse_position().normalized()) * 25
diggingRaycast.force_raycast_update()
if diggingRaycast.is_colliding():
BlockManager.blockErase(diggingRaycast.get_collision_point())

In BlockManager script

func blockErase(pos: Vector2):
coords = tml.local_to_map(tml.to_local(pos))
tml.erase_cell(coords)

Is the start position of the raycast located at the right place? Also, it only seems to cast for 25 pixels, is that enough? also, get_collision_point uses the global coordinate system, and i don’t know if that works with blockmanager

It’s often a good idea to put in some debug scaffolding. Maybe draw a dot at your rayacast endpoints to see if they’re showing up where you expected them to. Maybe print out the raycast coords and see if any of the numbers look wrong.

1 Like

i’ve fixed it

if diggingRaycast.is_colliding():
var collision_point = position + (diggingRaycast.get_collision_point() - position) * 1.05
BlockManager.blockErase(collision_point)

1 Like