Why are my cars glitching through walls?

I asked a similar question earlier on building support, cause this is kind of a physics-related question, but I figured asking this as a scripting question would fare better. The problem is whenever my car crashes into a fence or any wall, in particular, it just glitches through the fence/wall. How do I fix that? The car does collide with the wall, but it still glitches through.

Part of the script allowing the car to move:

runService:BindToRenderStep("Update", Enum.RenderPriority.Input.Value, function()
	if inCar then
		vehicle.PrimaryPart.CFrame = vehicle.PrimaryPart.CFrame + vehicle.PrimaryPart.CFrame.lookVector * moveSpeed
		vehicle.PrimaryPart.CFrame = vehicle.PrimaryPart.CFrame * CFrame.fromEulerAnglesXYZ(0, turnSpeed, 0)
	end
end)
1 Like

Ok so.
You are setting CFrame.
Maybe try using Body Movers.
Otherwise, you might need ray casting, and if there’s a hit return.
Like this:

runService:BindToRenderStep("Update", Enum.RenderPriority.Input.Value, function()

local hit = workspace:Raycast(vehicle.PrimaryPart.CFrame.Position,vehicle.PrimaryPart.CFrame.LookVector.Unit * 10,, RaycastParams.new(FilterDescendantsInstances = vehicle))
if hit then
print('found hit!')
return
end
	if inCar then
		vehicle.PrimaryPart.CFrame = vehicle.PrimaryPart.CFrame + vehicle.PrimaryPart.CFrame.lookVector * moveSpeed
		vehicle.PrimaryPart.CFrame = vehicle.PrimaryPart.CFrame * CFrame.fromEulerAnglesXYZ(0, turnSpeed, 0)
	end
end)
1 Like