Prevent clipping in car camera script

I have a camera script for a car and I want to prevent it from clipping through walls like so:

I have tried looking for help on this subject, however I could only find people implementing a fix for this with 3rd person player cameras that can be freely rotated, so none of the fixes work on my script.

My script itself is very basic; the camera is attached to the car’s CFrame with an added offset.

Any help is appreciated.

You’re gonna have to send a raycast from the back of the car with a ray direction facing backwards. If that raycast does hit something, place the camera where it hit, if it didn’t hit anything, move it using your current offset.

If you could show your code, I could help with implementation.