How to shapecast to a place on a 3d plane

Godot Version

4.4

Question

I’m trying to shapecast a sphere through a mesh to hit other meshes behind it from the camera. My X position of my target im trying to hit and the x target_position of my shapecast ray are correct, but the y and z are always massively off and I can never get the shapecast trigger it’s is_colliding check.

I’m not sure how to procceed as even with visible collision shapes turned on in the debug menu, I can never see my shapecast.

For example hovering directly my fish I get this:
fish pos(-42.10647, -4.45757, -14.7431)
target pos(-42.20139, -540.6368, -747.0784)

The y and z are way off, if I’m hovering over it, it should line up right?

Here’s the code that produces that result:

func _process(delta: float) -> void:
	var mouse_pos = get_viewport().get_mouse_position()
	var ray_origin = current_camera.project_ray_origin(mouse_pos)
	var ray_dir = current_camera.project_ray_normal(mouse_pos)
	var max_distance = 1000.0  

	shapecast.target_position = ray_origin + ray_dir * max_distance
	
	print(str("target pos", shapecast.target_position));
	print(str("fish pos", position))
	
	if shapecast.is_colliding():
		print ("colliding with something")

I think this means your cast is not in the visible field.

One thing to keep in mind the ShapeCast3D casting is done in local space to the ShapeCast node. So if you are assigning global space positions they will always be incorrect (unless the shapecast node is positioned at the world origin and has no rotation). You will need to call shapecast.target_position = shapecast.to_local(<target global position>)

From the docs on target_position:

The shape’s destination point, relative to this node’s Node3D.position.

The alternative is make a shape cast query that can be done in global space.

Hi, yes this is what I ended up doing as I was thinking about the raycasts a bit wrong, thanks :slight_smile: