Hello, I want to be able to control how much my car slides. The problem is I don’t know how to get started.
Here is a video of what I don’t want my car to do:
https://gyazo.com/87f90db3771dc2f46cded0478c3ad92c
second video:
https://gyazo.com/c011701732f5f1873ecfaeda1366db4b
I tried calculating the velocity in the axis I don’t want it to slide and using ApplyImpulse to try to stop the sliding but my calculations are wrong.
Here is the script I have right now:
local car = script:WaitForChild("Car").Value
local stop = script:WaitForChild("Stop")
local seat = car.Body.VehicleSeat
local wheelFL = car.Body.WheelFL.PhysicalWheel
local wheelFR = car.Body.WheelFR.PhysicalWheel
local wheelRR = car.Body.WheelRR.PhysicalWheel
local wheelRL = car.Body.WheelRL.PhysicalWheel
local mass = car.Chasis.Mass
local attFL = car.Chasis.Platform.AttachmentFL
local attFR = car.Chasis.Platform.AttachmentFR
local attRR = car.Chasis.Platform.AttachmentRR
local attRL = car.Chasis.Platform.AttachmentRL
local cyFL = car.Chasis.Platform.CylindricalFL
local cyFR = car.Chasis.Platform.CylindricalFR
local cyRL = car.Chasis.Platform.CylindricalRL
local cyRR = car.Chasis.Platform.CylindricalRR
local maxSteerAngle = 50
local steer = 0
local throttle = 0
local tireGripFactor = 0.5
local heartbeat
local function Update(dt)
-- Steer:
local steerGoal = -seat.SteerFloat * maxSteerAngle
steer = steer + (steerGoal - steer) * math.min(dt * seat.TurnSpeed, 1)
local radians = math.rad(90 - math.abs(steer))
local h =(attFR.WorldPosition - attRR.WorldPosition).Magnitude
local z = (attRR.WorldPosition - attRL.WorldPosition).Magnitude
local x = math.tan(radians) * h
local y = (x + z)
local outerTurnAngle = 90 - math.deg(math.atan2(y, h))
if (steer > 0) then
attFL.Orientation = Vector3.new(0, steer, -90)
attFR.Orientation = Vector3.new(0, outerTurnAngle, -90)
else
-- Right
outerTurnAngle = -outerTurnAngle
attFL.Orientation = Vector3.new(0, outerTurnAngle, -90)
attFR.Orientation = Vector3.new(0, steer, -90)
end
--<< THE CODE THAT DOES NOT WORK >>--
-- Wheel grip/Prevent sliding
local wheelGlobalVelocity = wheelFL.AssemblyLinearVelocity
local direction = wheelFL.CFrame.RightVector
local velocityRight = wheelGlobalVelocity:Dot(direction)
local desiredVelChange = -velocityRight * tireGripFactor
local acceleration = desiredVelChange / dt
local force = direction * mass.AssemblyMass * acceleration
wheelFL:ApplyImpulse(force)
--<< END >>--
-- Throttle:
local throttleGoal = seat.ThrottleFloat
throttle = throttle + (throttleGoal - throttle) * math.min(dt * seat.TurnSpeed, 1)
local torque = seat.Torque
local speed = seat.MaxSpeed * throttle
--print(wheelFL.Velocity)
cyFL.MotorMaxTorque = torque
cyFR.MotorMaxTorque = torque
cyRL.MotorMaxTorque = torque
cyRR.MotorMaxTorque = torque
cyFL.AngularVelocity = speed
cyFR.AngularVelocity = -speed
cyRL.AngularVelocity = speed
cyRR.AngularVelocity = -speed
end
Any help is appreciated!