I am working on a game in which you drive with carts on a track from node to node. On the server, the player positions are stored as simple parts that are updated every frame. On the client, my code goes through all active players and lerps their carts to the correct position. Sometimes I get minor stuttering on mobile, and with the microprofiler I noticed that its the lerping function which i gave the name of ‘lerping’. The function is bound to bindtorenderstep like this: runService:BindToRenderStep('animationLoop',Enum.RenderPriority.Camera.Value-1,syncLoop)
This is the full function:
function syncLoop(delta)
deltaCount += delta
debug.profilebegin('AnimCarts')
for _,folder in ipairs(animatePlrsList) do
local cart
local s,e = pcall(function()
cart = folder.Cart :: Model
end)
if not s then continue end
debug.profilebegin('cartPos')
local posPart = folder:WaitForChild('CartPosition') :: BasePart
local lerpSpeed = 9 -- Lower is smoother, but lags behind the actual position
local lerpAlpha = math.clamp(delta * lerpSpeed, 0, 1)
local currentPivot = cart:GetPivot()
local standStill = folder:GetAttribute('stillIntersection')
debug.profilebegin('targetPivot')
local targetPivot = posPart.CFrame * CFrame.new(0,-posPart.Position.Y + cart:GetAttribute('FloorDistance') + floorHeight,0)
if folder.Name == ownFolderName and not standStill then
targetPivot *= CFrame.new(0,0,-2) --makes coyote time
end
debug.profileend()
debug.profilebegin('Lerping')
local lerpedPivot = currentPivot:Lerp(targetPivot, lerpAlpha)
cart:PivotTo(lerpedPivot)
debug.profileend()
debug.profileend()
debug.profilebegin('Wheel')
local diameter = cart.Wheels:GetAttribute('Diameter')
local distTraveled = folder:GetAttribute('CurrentSpeed')*delta
if not standStill then
for _,wheel in ipairs(cart.Wheels:GetChildren()) do
local reverser = 1
local wheelName = wheel.Name
if string.sub(wheelName,#wheelName) == 'R' then
reverser = -1
end
wheel:PivotTo(wheel.WorldPivot * CFrame.Angles(math.rad(distTraveled/diameter * 360 * reverser),0,0))
end
end
debug.profileend()
end
debug.profileend()
debug.profilebegin('AnimProjectiles')
for _,folder in ipairs(animateProjectilesList) do
local proj = folder.Projectile
local posPart = folder:WaitForChild('PosPart') :: BasePart
local lerpSpeed = 15
local lerpAlpha = math.clamp(delta * lerpSpeed, 0, 1)
local currentPivot = proj:GetPivot()
local targetPivot = posPart.CFrame * CFrame.new(0,projectileHeight,0)
local lerpedPivot = currentPivot:Lerp(targetPivot, lerpAlpha)
proj:PivotTo(lerpedPivot)
end
debug.profileend()
local curve = math.sin(tick()*bobSpeed + 0.5)
debug.profilebegin('PowerUpsBob')
for _,model in ipairs(animatePowerUpList) do
local basePos = model.WorldPivot.Position
local pos = Vector3.new(basePos.X,powerUpheight+curve*bobHeight,basePos.Z)
local rX,rY,rZ = model.WorldPivot:ToOrientation()
model:PivotTo(CFrame.new(pos) * CFrame.Angles(rX,rY+math.rad(-1*delta*60),rZ))
end
debug.profileend()
debug.profilebegin('EngineShake')
if deltaCount > 0.05 and ownCart then
local offset = ownCart.Engine:GetAttribute('Offset')
ownCart.Engine:PivotTo(ownCart.EngineAttachment.WorldCFrame * offset:Inverse() * CFrame.new(math.random(-engineShake*100,engineShake*100)/100,math.random(-engineShake*100,engineShake*100)/100,0))
deltaCount = 0
end
debug.profileend()
end
This is the microprofiler tab of the Lerping debug part:
What can I do to reduce the stuttering on mobile? If I remove the lerping, the carts will move jittery.
