Most performant way to animate a camera constantly?

So for my game objects currently, I use pivot points and the SetPivot method to animate the models’ CFrames. This works fine for the majority of objects, but noticed for the cameras I have (there are quite a few, and they all move constantly) they’re causing script activity to spike around 3-4%. This is also tied to frame rate, so if the user is playing on 240 hertz the script activity continues to jump even more, which makes sense. I could restrain it, but it still is pretty rough even tied on 60 fps.

What would be the optimal way to implement a constant left-right animation for the cameras? I figured some physical constraint would be best for constant physics, but I don’t really know how to implement it.

Thanks a lot.

For simple camera systems, I usually just lerp the camera’s CFrame on RenderStepped towards its goal. It feels smoother than setting the CFrame or Pivoting directly, because it gets a point in between the given position and its goal.

With lerping, I don’t think it ever truly reaches its goal, however, so I’d recommend just setting it to its goalpoint once it’s within a short magnitude.

RunSVC:BindToRenderStep("Camera", 0, function()

    local camCFrame = camera.CFrame

    local distFromGoal = (camCFrame.Position - goalCFrame.Position).Magnitude

    camera.CFrame = distFromGoal < 0.01 and goalCFrame
        or camera.CFrame:lerp(goalCFrame, 0.1) -- range is 0-1, determines speed

end)
1 Like