Hello. I have some humanoid based vehicles that use Vectorforce to move and AngularVelocity to steer. However there is very much drifting as in although the Angle has changed the Vectorforce continues in the previous direction for a while before adjusting.
I’d like to introduce Centripedal force to correct this. I’m not sure how to implement it yet, I wouldn’t mind some advice.
Here is the code:
function WorldMovingDirection(dir, MultiDirectional)
--//Made by Juni_Purr and ImPremiumJay
local NewMultiDirectional = MultiDirectional or true
local angle = math.atan2(dir.X, -dir.Z)--Gives Angle
local quarterTurn
if NewMultiDirectional then--If you want 8 Directional
quarterTurn = math.pi/4
else--You want 4 Directional
quarterTurn = math.pi/2--Only want degrees 0-180 [x/2 is 4 dimensional, x/4 is 8 dimensional]
end
local CurrectVector = nil
local MovingDirection
if dir == Vector3.new(0,0,0) then return 'Idle' end--If they are standing still they are idle
angle = -math.round(angle / quarterTurn) * quarterTurn
local newX =math.round(-math.sin(angle))
local newZ = math.round(-math.cos(angle))
if math.abs(newX) <= 1e-10 then newX = 0 end
if math.abs(newZ) <= 1e-10 then newZ = 0 end
CurrectVector = Vector3.new(newX, 0, newZ)
--Dictionary so you dont have to read numbers
local Directions = {
['Forward'] = Vector3.new(0, 0, -1),
['Left'] = Vector3.new(-1, 0, 0),
['Backwards'] = Vector3.new(0, 0, 1),
['Right'] = Vector3.new(1, 0, 0),
['Idle'] = Vector3.new(0, 0, 0)
}
if NewMultiDirectional then--If you want 8 directional movement
Directions["ForwardLeft"] = Vector3.new(-1, 0, -1)
Directions["ForwardRight"] = Vector3.new(1, 0, -1)
Directions["BackwardLeft"] = Vector3.new(-1, 0, 1)
Directions["BackwardRight"] = Vector3.new(1, 0, 1)
end
for Direction, Vector in Directions do
-- Compare Directions
if CurrectVector == Vector then--If players vector equals a vector in table Directions
MovingDirection = Direction--Im not explaining this
end
end
return MovingDirection
end
Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
if HRP.VehicleMode.Value == true and HRP.Flying.Value == false then
Humanoid.JumpPower = 0
HRP.AngularVelocity.Enabled = true
HRP.VectorForce.Enabled = true
local movementDirection = WorldMovingDirection(HRP.CFrame:VectorToObjectSpace(Humanoid.MoveDirection))
print(movementDirection)
if movementDirection == "Left" then
if HRP.VectorForce.Force ~= Vector3.new(0, 0, 0) then
HRP.AngularVelocity.AngularVelocity = Vector3.new(0, 2, 0)
end
end
if movementDirection == "ForwardLeft" then
VehicleIdle:Pause()
if not VehicleDrivingFast.IsPlaying then
VehicleDrivingFast:Play()
end
HRP.AngularVelocity.AngularVelocity = Vector3.new(0, 2, 0)
HRP.VectorForce.Force = Vector3.new(0, 0, -1600)
end
if movementDirection == "Right" then
if HRP.VectorForce.Force ~= Vector3.new(0, 0, 0) then
HRP.AngularVelocity.AngularVelocity = Vector3.new(0, -2, 0)
end
end
if movementDirection == "ForwardRight" then
VehicleIdle:Pause()
if not VehicleDrivingFast.IsPlaying then
VehicleDrivingFast:Play()
end
HRP.AngularVelocity.AngularVelocity = Vector3.new(0, -2, 0)
HRP.VectorForce.Force = Vector3.new(0, 0, -1600)
end
if movementDirection == "Idle" then
VehicleDrivingSlow:Pause()
VehicleDrivingFast:Pause()
VehicleIdle:Play()
HRP.AngularVelocity.AngularVelocity = Vector3.new(0, 0, 0)
HRP.VectorForce.Force = Vector3.new(0, 0, 0)
end
if movementDirection == "Forward" then
VehicleIdle:Pause()
if not VehicleDrivingFast.IsPlaying then
VehicleDrivingFast:Play()
end
HRP.AngularVelocity.AngularVelocity = Vector3.new(0, 0, 0)
HRP.VectorForce.Force = Vector3.new(0, 0, -1600)
end
if movementDirection == "Backwards" then
VehicleIdle:Pause()
VehicleDrivingFast:Pause()
if not VehicleDrivingSlow.IsPlaying then
VehicleDrivingSlow:Play()
end
HRP.VectorForce.Force = Vector3.new(0, 0, 1600)
end
elseif HRP.VehicleMode.Value == false and HRP.Flying.Value == false then
Humanoid.JumpPower = 80
end
end)