When riding on the skateboard, when the player turns, the board leans to the direction of turn. I want the player to lean as well. I’ve used CFrames on the humanoidrootpart to lean the player but when I do that the player glitches back and forth between the normal position and lean creating a glitchy turn. Any ideas on how to approach this?
With the player lean:
Without player lean:
function SkateboardController:StartMovementLoop(player)
if MovementConnections[player] then return end
PlayerInputState[player] = {W = false, S = false}
PlayerDirections[player] = Vector3.new(0, 0, -1)
PlayerSpeeds[player] = PlayerSpeeds[player] or 0
local connection
connection = RunService.Heartbeat:Connect(function(dt)
local character = player.Character
if not character or not PlayerStates[player] then
connection:Disconnect()
MovementConnections[player] = nil
return
end
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
local humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoidRootPart or not humanoid then
connection:Disconnect()
MovementConnections[player] = nil
return
end
local inputState = PlayerInputState[player] or {W = false, S = false}
local dir = PlayerDirections[player] or Vector3.new(0,0,-1)
if dir.Magnitude == 0 then dir = Vector3.new(0,0,-1) end
dir = Vector3.new(dir.X, 0, dir.Z)
if dir.Magnitude > 0 then dir = dir.Unit end
local grounded = self:IsGrounded(character)
local currentSpeed = PlayerSpeeds[player] or 0
if inputState.W and grounded then
if currentSpeed < maxSkateboardspeed then
currentSpeed = math.min(maxSkateboardspeed, currentSpeed + accelerationgSpeed * dt)
end
elseif inputState.S then
currentSpeed = math.max(0, currentSpeed - Brake * dt)
else
currentSpeed = math.max(0, currentSpeed - coastingdecrease * dt)
end
PlayerSpeeds[player] = currentSpeed
if currentSpeed > 0 then
local currentLook = humanoidRootPart.CFrame.LookVector
local desiredDir = dir
if inputState.W then
local angleDiff = math.acos(math.clamp(currentLook:Dot(desiredDir), -1, 1))
local maxTurn = math.rad(SkateboardTurn) * dt
if angleDiff > maxTurn then
local axis = currentLook:Cross(desiredDir)
if axis.Magnitude > 0 then
axis = axis.Unit
desiredDir = CFrame.fromAxisAngle(axis, maxTurn) * currentLook
else
desiredDir = currentLook
end
end
else
desiredDir = currentLook
end
local moveDelta = desiredDir * currentSpeed * dt
local newPos = humanoidRootPart.Position + moveDelta
humanoidRootPart.CFrame = CFrame.lookAt(newPos, newPos + desiredDir)
local motor = SkateboardWelds[player]
if motor then
local turnDir = currentLook:Cross(desiredDir).Y
local lean = math.clamp(turnDir * 30, -maxLean, maxLean)
local currentC0 = motor.C0
local targetC0 = CFrame.new(currentC0.Position) * CFrame.Angles(math.rad(90), lean, 0)
motor.C0 = currentC0:Lerp(targetC0, dt * 5)
local playerLeanAmount = lean * PlayerLean
humanoidRootPart.CFrame = CFrame.lookAt( --Player lean code
humanoidRootPart.Position,
humanoidRootPart.Position + desiredDir,
Vector3.new(0,1,0)
) * CFrame.Angles(0, 0, -playerLeanAmount)
end
end
if currentSpeed < 1 then
if PlayerStates[player] and not PlayerInputState[player].W then
self:StopAnimations(character)
self:PlayAnimation(character, "Idle")
end
end
end)
MovementConnections[player] = connection
end