Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

What am I doing wrong with deltaTime and why is the camera stuttering?

Asked by 2 years ago
Edited 2 years ago

Greetings. I am attempting to make the Camera lerp to an object that it is tracking.

The object is controlled locally and uses BodyVelocity for it to move for the sake of example.

This is my code (very sloppy/pseudo code, please forgive):

-- Please excuse the messy code
local RS = game:GetService("RunService");

local Goal = script.Goal;
local Camera = workspace.CurrentCamera;

Goal.Parent = workspace;

local CONST = 0.016666666666666667;

RS:BindToRenderStep("Test", Enum.RenderPriority.Camera.Value + 1, function (dt)
    local delta = dt/CONST; -- This just make it work with variable framerates if using an unlocker, does not afffect jittering     

    local p = Goal.Position + Vector3.new(1,3,2);
    local final = CFrame.new(p, Goal.Position);

    Camera.CameraType = Enum.CameraType.Scriptable;

    local speed = 0.5;

    local rot = Camera.CFrame.Rotation;
    Camera.CFrame = Camera.CFrame:Lerp(final, speed * delta); -- Introduces jitter, with or without delta
    --Camera.CFrame = final; -- Does not have jitter
end)

The result is that the faster the object moves, the more the camera vibrates/jitters.

Normally I'd assume the solution would be to add delta time in but that's what I'm doing.

Camera.CFrame = Camera.CFrame:Lerp(New, Speed * Delta)

On top of that, it's in :BindToRenderStep() loop set to Enum.RenderPriority.Camera.Value + 1. So I'm not really sure what else I could be doing here to fix the stuttering. I've even tried using heartbeat delta, stepped delta, adding them together, etc, and it only very minutely improves the stuttering.

I should also mention that lerp() is important here. It's not very apparent since this example shows the object moving at a custom speed but when it's attached to an object that moves around at varying speeds like a player or vehicle, it improves the look and feel.

Uncopylocked place

So what could I be doing wrong here and what could I do to fix it? I'm pretty stumped. I should also note that I'm not a novice so I'm unsure if I'm having a major brain fart or if this is actually a complex issue, I'm guessing the former.

Answer this question