Detecting rate of change

Hey everyone!

This is more of a logic question. I am trying to apply a camera shake animation when a vehicle collides with something. My idea was to detect if the Rate of Change of speed exceeds a certain threshold to determine if the vehicle has collided. Currently I am using a Value.Changed function (with the value being speed) to perform other things. How would I detect the rate of which the speed value is changing?

Thanks

1 Like

I haven’t tested this.
Edit: I realised it wouldn’t work so I changed it a little. I’m still not sure if it works.

local RunService = game:GetService("RunService")

local COLLISION_ACCELERATION = -- should be negative, because colliding reduces speed

local speedValObj = -- the value instance

local latestSpeed = speedValObj.Value

local function checkAccel(dt)
.   local newSpeed = speedValObj.Value
    local acceleration = (newSpeed-latestSpeed)/dt
    latestSpeed = newSpeed
    if acceleration < collisionAcceleration then
        -- your code
    end
end

RunService.Heartbeat:Connect(checkAccel)
2 Likes