Raycasts working poorly on a custom Collision Shape

Godot Version

4.2.1

Question

I’m making a racing game and I want the car to rotate to match the ground. I used a Raycast to detect the ground’s normal and then apply it to the car’s rotation, and it seemed like it worked, but it goes by kind of chunks. I don’t know how to explain it, here’s a video of what happens:


(please ignore the sound in the video)

Also, this is the code I used: car_mesh.rotation.x = raycast.get_collision_normal().z
To make it smoother I then used lerp: car_mesh.rotation.x = lerp(car_mesh.rotation.x, raycast.get_collision_normal().z, delta * 3)

But it still goes by chunks.

My theory is that the raycast is not detecting the ground’s normal all the time, but I’m not sure. Any idea?

You single ray is hitting edge seams or internal edges which is a normal physics problem that gets extra amplified by certain geometry types.

You need to use more than one ray and have them spaced out so they hit different positions. Then use that information from all those rays, discard those that clearly had errors, and figure out the correct “normal” from those that make sense. That result is what you want to use for your car placement.

1 Like

Thanks! I don’t know how to “discard” the raycasts that are not working, but I’ll try anyways :slight_smile: