Hello everyone I was working on a bike system, but I can’t sort why it’s bouncing up/down like that.
This happens:
here’s code!
local player = game.Players.LocalPlayer
local character = player.Character
local humanoidRootPart = character.HumanoidRootPart
local Vehicle = script:WaitForChild("Vehicle").Value
local VehicleInput = Vector2.new(0, 0)
local Wheels = {
{
Part = Vehicle.FR,
Tire = Vehicle.FR_Tire,
Steer = true,
SteerAngle = 35,
Suspension = {
Length = 3.5,
Strength = 3600,
Damper = .5
},
compressionDistance = 0
},
{
Part = Vehicle.RR,
Tire = Vehicle.RR_Tire,
Suspension = {
Length = 3.5,
Strength = 3600,
Damper = .5
},
compressionDistance = 0
}
}
Vehicle.PrimaryPart.Anchored = false
script.ScreenGui.Bar.VehicleName.Text = Vehicle.Configuration.VehicleName.Value
game.Workspace.Camera.CameraType = Enum.CameraType.Custom
game.Workspace.Camera.CameraSubject = Vehicle.MainPart
game:GetService("RunService").Heartbeat:Connect(function(step)
Vehicle.FakeMainPart.CFrame = Vehicle.MainPart.CFrame
local inputX = 0
local inputY = 0
if UserInputService:IsKeyDown(Enum.KeyCode.W) then
inputY = 1
elseif UserInputService:IsKeyDown(Enum.KeyCode.S) then
inputY = -1
end
if UserInputService:IsKeyDown(Enum.KeyCode.D) then
inputX = 1
elseif UserInputService:IsKeyDown(Enum.KeyCode.A) then
inputX = -1
end
VehicleInput = Vector2.new(inputX, inputY)
local velocityVector = Vehicle.MainPart.CFrame:vectorToObjectSpace(Vehicle.MainPart.Velocity)
local circumference = 2.242 * math.pi * 1.5
for _, wheel in pairs(Wheels) do
local raycastParams = RaycastParams.new()
local partsToIgnore = Vehicle.Chassis:GetChildren()
table.insert(partsToIgnore, wheel.Tire)
table.insert(partsToIgnore, wheel.Part)
raycastParams.FilterDescendantsInstances = partsToIgnore
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local ray = workspace:Raycast(wheel.Part.Position, -wheel.Part.CFrame.UpVector * wheel.Suspension.Length, raycastParams)
if ray then
local rayDistance = math.clamp((wheel.Part.Position - ray.Position).Magnitude, 0, wheel.Suspension.Length)
local compressionRatio = 1 - (rayDistance / wheel.Suspension.Length)
local compressionForce = wheel.Suspension.Strength * compressionRatio
local rate = wheel.compressionDistance - rayDistance
wheel.compressionDistance = rayDistance
local dampingForce = rate * wheel.Suspension.Strength * wheel.Suspension.Damper
local finalForce = compressionForce - dampingForce
local forwardVelocity = wheel.Part.Velocity:Dot(wheel.Tire.CFrame.LookVector)
local lateralVelocity = wheel.Part.Velocity:Dot(wheel.Tire.CFrame.RightVector)
wheel.Part.BodyThrust.Force = (Vector3.new(0, 1, 0) * finalForce)
wheel.Part.BodyThrust.Force -= wheel.Tire.CFrame:VectorToObjectSpace(wheel.Tire.CFrame.LookVector * (lateralVelocity * 10))
wheel.Part.BodyThrust.Force -= wheel.Tire.CFrame:VectorToObjectSpace(wheel.Tire.CFrame.RightVector * (velocityVector.X * 10))
--wheel.Part.BodyThrust.Location = wheel.Part.CFrame:PointToObjectSpace(ray.Position)
wheel.Part.Smoke.Enabled = (math.abs(lateralVelocity) > 10) and true or false
wheel.Tire.CFrame = wheel.Part.CFrame * CFrame.Angles(0, math.rad(90 + -(VehicleInput.X) * (wheel.Steer and wheel.SteerAngle or 0)), 0) - wheel.Part.CFrame.UpVector * (rayDistance - 2.242 / 2)
--if wheel.Steer then
-- wheel.Tire.CFrame = wheel.Part.CFrame * CFrame.Angles(0, math.rad(90 + -(VehicleInput.X) * wheel.SteerAngle), 0) - wheel.Part.CFrame.UpVector * (rayDistance - 2.242 / 2)
--else
-- local localCFrame = wheel.Tire.CFrame:ToObjectSpace(wheel.Part.CFrame)
-- local angleX, angleY, angleZ = localCFrame:ToEulerAnglesXYZ()
-- wheel.Tire.CFrame = wheel.Part.CFrame * CFrame.Angles(0, math.rad(90), 0) * CFrame.Angles(velocityVector.X / circumference, 0, 0)
--end
else
wheel.Part.BodyThrust.Force = Vector3.new(0, 1, 0)
wheel.compressionDistance = wheel.Suspension.Length
end
end
Vehicle.MainPart.BodyForce.Force = Vehicle.MainPart.CFrame.RightVector * VehicleInput.Y * 10
script.ScreenGui.Bar.Progress.Size = UDim2.new(math.max(0.09, math.abs(velocityVector.X) / 100), 0, 1, 0)
script.ScreenGui.Bar.Progress.TextLabel.Text = math.floor(math.abs(velocityVector.X))
--if character.Humanoid.SeatPart ~= Vehicle.Seat then
-- Vehicle.PrimaryPart.Anchored = true
-- script:Destroy()
--end
end)