Hello!
I was making a hoverboard and basically I’m not really a physics person…
Can someone help me figure out why this is drifting (when I turn left and right) and how to fix it? (this also happens sometimes when it’s just going straight, like in the video).
Thanks in advance!
local Humanoid = Character:WaitForChild("Humanoid")
local RunService = game:GetService("RunService")
local Connection = nil
local HoverboardSeat = nil
local Attachment = nil
local VectorForce = nil
local Orientation = nil
local AlignOrientation = nil
local RaycastParamaters = nil
local RayOffset = Vector3.new(0, -8, 0)
local RayVelocity = 0.2
local ThrustMin = 600
local Thrust = 2000
local Drag = 0.008
local Counter = 0.004
local AniamtionTrack = Humanoid:WaitForChild("Animator"):LoadAnimation(workspace.Hoverboard:WaitForChild("Hovering"))
AniamtionTrack.Priority = Enum.AnimationPriority.Idle
local function Loop(DeltaTime)
local RayTarget = HoverboardSeat.Position + RayOffset + HoverboardSeat.AssemblyLinearVelocity * RayVelocity
local RaycastResult = workspace:Raycast(HoverboardSeat.Position, RayTarget - HoverboardSeat.Position, RaycastParamaters)
if not RaycastResult then
VectorForce.Force = Vector3.new(0, ThrustMin, 0)
else
local Magnitude = (RaycastResult.Position - RayTarget).Magnitude
VectorForce.Force = Vector3.new(0, ThrustMin + Magnitude * Thrust, 0)
end
local Velocity = HoverboardSeat.CFrame:VectorToObjectSpace(HoverboardSeat.AssemblyLinearVelocity)
if Velocity.Magnitude > 0 then
VectorForce.Force -= Velocity.Unit * (Drag * Velocity.Magnitude ^ 2)
else
VectorForce.Force = Vector3.zero
end
Orientation *= CFrame.fromOrientation(0, -HoverboardSeat.SteerFloat * HoverboardSeat.TurnSpeed * DeltaTime, 0)
local Tilt = CFrame.fromOrientation(-HoverboardSeat.ThrottleFloat * HoverboardSeat.Torque, 0, -HoverboardSeat.SteerFloat * HoverboardSeat.Torque)
local CounterX = math.clamp(-HoverboardSeat.AssemblyLinearVelocity.Z * Counter, -HoverboardSeat.Torque, HoverboardSeat.Torque)
local CounterZ = math.clamp(-HoverboardSeat.AssemblyLinearVelocity.X * Counter, -HoverboardSeat.Torque, HoverboardSeat.Torque)
local CounterOrientation = CFrame.fromOrientation(CounterX, 0, CounterZ)
AlignOrientation.CFrame = CounterOrientation * Orientation * Tilt
end
local function Seated(Active, Seat)
if not Active then
if Connection then
Connection:Disconnect()
Connection = nil
Attachment:Destroy()
VectorForce:Destroy()
AlignOrientation:Destroy()
AniamtionTrack:Stop()
end
elseif Seat.Name == "Hoverboard" then
HoverboardSeat = Seat
VectorForce = Instance.new("VectorForce")
Attachment = Instance.new("Attachment", Seat)
VectorForce.Force = Vector3.zero
VectorForce.ApplyAtCenterOfMass = true
VectorForce.Attachment0 = Attachment
VectorForce.Parent = Seat
Orientation = CFrame.fromMatrix(Vector3.zero, Seat.CFrame.LookVector:Cross(Vector3.yAxis), Vector3.yAxis)
AlignOrientation = Instance.new("AlignOrientation")
AlignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
AlignOrientation.CFrame = Orientation
AlignOrientation.Attachment0 = Attachment
AlignOrientation.Parent = Seat
RaycastParamaters = RaycastParams.new()
RaycastParamaters.FilterType = Enum.RaycastFilterType.Exclude
RaycastParamaters.FilterDescendantsInstances = {Character, Seat:FindFirstAncestorOfClass("Model")}
AniamtionTrack:Play()
Connection = RunService.PostSimulation:Connect(Loop)
end
end
Humanoid.Seated:Connect(Seated)