To be more specific, I want to detect how hard the hit is, and depending on how hard the hit was either make something explode, go on fire or break welds.
Here I leave you the main part and I attach an example.
part.Touched:Connect(function(hit)
local p = (part.Position + hit.Position) / 2
local int = (part.Velocity - hit.Velocity).Magnitude
local exp = Instance.new("Explosion")
exp.Position = p
exp.BlastRadius = int
exp.BlastPressure = int
exp.Parent = game.Workspace
end)
I attach again the example changing the velocity of the bodies.
local part = game.Workspace.P1
local vel = 10
local fact_radius = 2
local fact_pressure = 10
part.Touched:Connect(function(hit)
local p = (part.Position + hit.Position) / 2
local int = (part.Velocity - hit.Velocity).Magnitude
local exp = Instance.new("Explosion")
print(int)
exp.Position = p
exp.BlastRadius = int * fact_radius
exp.BlastPressure = int * fact_pressure
exp.Parent = game.Workspace
end)
wait(20)
game.Workspace.P1.Velocity = Vector3.new(vel, 0, 0)
game.Workspace.P2.Velocity = -Vector3.new(vel, 0, 0)
The formula for calculating force is Force = Mass * Change in Velocity . You can get the velocity of a part using the Velocity property.
Here’s a bit of example code that should help get you started in the right direction:
local function onCollision(otherPart)
local impactThreshold = 1000 -- Adjust this value based on your needs
-- Calculate impact force
local velocity = vehiclePart.Velocity
local mass = vehiclePart:GetMass()
local impactForce = mass * velocity.Magnitude
-- Check if impact force is above the threshold
if impactForce >= impactThreshold then
-- Perform actions based on impact force
if impactForce >= explosionThreshold then
-- Create an explosion
local explosion = Instance.new("Explosion")
explosion.Position = vehiclePart.Position
explosion.BlastRadius = 10
explosion.BlastPressure = impactForce / 1000 -- Adjust as needed
explosion.Parent = game.Workspace
explosion:Fire()
elseif impactForce >= fireThreshold then
-- Insert code to start a fire effect here
else
-- Insert code to break welds or other actions here
end
end
end
-- Connect the collision event
vehiclePart.Touched:Connect(onCollision)
How could I detect if a different car hit my car at high speed and to break the parts on my car?
Also with the code you provided I’m not able to replicate the result onto a moving vehicle controlled by the player, it seems to work though on a free falling part in the workspace.
This is the part where you exercise creative liberty as a programmer, as there are several ways to detect a different car hitting yours. The code provided, as mentioned, is just some sample code to get you started in the right direction.
As stated, it is sample code that is best used for instruction and learning, and may need tweaking to fit your needs. You did not provide any sample code of your exact scenario, so I figured this was the best I could do.
It may be that my code doesn’t work or that the functionalities aren’t being properly implemented, as the code is very minimal and may not have properly accounted for several variables I didn’t know about. I only code a little bit, and to my knowledge the code should be functional if not rudimentary, but my knowledge is limited because I mostly 3d model, so I do apologize for that.