I have a system where you can shoot out cars, and I have just recently converted it to just constantly change a SeatWeld C0 instead of setting HumanoidRootPart CFrame directly, because if I did the latter, the whole car would move because the SeatWeld is welded to the HumanoidRootPart and the seat, obviously, and the seat itself is welded to the car.
Anyway, I did some CFrame math to convert it and it works perfectly, but now it drops my FPS by about 20-35 frames.
This code is binded to render steps. I know the cause of the lag is because of the CFrame math, but why? This doesn’t look intense at all, and it’s not that different from the old code.
Old relevant code snippet (binded to RenderStep)
root.CFrame = CFrame.new(root.Position, root.Position + camera.CFrame.LookVector) * CFrame.Angles(0, 0, -math.rad(rotAngle))
New relevant code snippet (also binded to render step, this is what the old one was replaced by)
local origTarget = CFrame.new(root.Position, root.Position + camera.CFrame.LookVector) * CFrame.Angles(0, 0, -math.rad(rotAngle))
local targetRootCF = CFrame.new(c0.p, c0.p + camera.CFrame.LookVector) * CFrame.Angles(0, 0, -math.rad(rotAngle))
local rel = seat.CFrame * targetRootCF
seatWeld.C0 = targetRootCF * rel:ToObjectSpace(origTarget)
currentC0 = seatWeld.C0
Of course, staying still stops the lag. But when I look around, my FPS drops, and the MicroProfiler shows these firing up when my FPS starts dropping:
How do I fix this?