How to detect if a vehicle crashes without it detecting sliding/scraping against a wall?

So right now I am developing a simple damage system for vehicle depending on how hard they crash into something.

At the moment, my current system uses .Touched() and calculates the impact depending on the speed of the vehicle and the speed of the object hit. All though this current implementation only takes direct impacts into account and will damage the car a lot when sliding or lighting scraping the surface of walls.

How do I fix this?

local function Touched(Hit)
	if Hit.CanCollide then
        -- Take other vehicles/loose objects into account when colliding
		local HitSpeed = (DriveSeat.AssemblyLinearVelocity - Hit.AssemblyLinearVelocity).Magnitude		

		local DamageToApply = CurrentVelocity / 4
		
		if not Crashing then
			Crashing = true
			CrashSound:Play()
			
			if HitSpeed > 30 then
				Health.Value -= DamageToApply
			end
			
			task.wait(0.4)
			Crashing = false
		end
	end
end

Can’t you just check the deceleration value of the DriveSeat.AssemblyLinearVelocity? If it’s above a certain percentage of the previous value then do damage according to that.

For example, dead stop:
Speed before impact: 100 studs/sec
Speed after impact: 0 studs/sec
Damage is 100-0 = 100

Heavy hit:
Speed before impact: 60 studs/second
Speed after impact: 35 studs/second
Damage is (60-35) = 25

Glancing blow:
Speed before impact: 10 studs/second
Speed after impact: 8 studs/second
Damage is 10-8 = 2

Also you could do a scale system where if the top speed of any vehicle in your game is 300 studs per second and your maximum vehicle health is 100 then you could divide the damage by 3 to keep it even in a dead stop crash.

1 Like

I would probably have different collision hitboxes throughout the car. You can tell the difference between an impact and scraping by direction the vehicle is moving in compared to the offset direction of the object impacted.

Oh, how have I not thought about comparing the speed before and after the impact. This works perfectly!

2 Likes