It is beyond creepy how I automatically solve code in my sleep but here we go the autocomplete gave me a hint at the end for the exact syntax. I feel like this is definitely a bit of a hacky solution to the problem, I'm sure there are better ways to do this more smoothly, I'm not sure I like these changes lol, thanks for the hints @spacecloud they definitely helped.
var spaceState = get_world_3d().direct_space_state
var mousePosition = get_viewport().get_mouse_position()
var raycastOrigin = playerCamera.project_ray_origin(mousePosition)
var raycastTarget = raycastOrigin + playerCamera.project_ray_normal(mousePosition) * rayLength
var physicsRaycastQuery = PhysicsRayQueryParameters3D.create(raycastOrigin, raycastTarget)
var raycastResult = spaceState.intersect_ray(physicsRaycastQuery)
if raycastResult.is_empty():
return
else:
global_transform.origin = raycastResult["position"]
The issue now is when my mouse goes above the terrain it stops which is pretty jarring, but I think this can be solved with a sphere collider or something as a work around. A common trick for shooters so even if you're firing into an empty space the game still works smoothly. Hopefully this code should at least get people on the right track if they're struggling with the new raycast system like I have been, at least I got rid of any major errors.
Or maybe actually a few box colliders to make the raycasts think you're in a room? That seems more practical.