How do I use delta time for my camera effect

So pretty much I’m making a camera effect to simulate nausea and it’s working fine but I want it to be the same speed for any framerate. I know I need to use delta time but I don’t know how to implement it.

I tried multiplying the R01 and R10’s final results by delta time which didn’t work and I also tried dividing which didn’t either. I was looking at other devforum posts about delta time but none of them helped so I’d figure I would post.

Here is my code (without delta time implemented yet):

local runService = game:GetService("RunService")
local camera = workspace.CurrentCamera

local DEGREE = 0
local INTENSITY = 0.2

local function DistortCamera(delta)
	if DEGREE >= 360 then DEGREE = 0 end
	DEGREE += 1
	
	local R01 = INTENSITY * math.cos(math.rad(DEGREE))
	local R10 = INTENSITY * math.sin(math.rad(DEGREE))
	
	local newCFrame = CFrame.new(0, 0, 0, 1, R01, 0, R10, 1, 0, 0, 0, 1)
	camera.CFrame = camera.CFrame * newCFrame
end

runService:BindToRenderStep("Nausea", Enum.RenderPriority.Camera.Value, DistortCamera)

Any help would be greatly appreciated so thanks in advance!

3 Likes

DeltaTime just refers to the time in between frames. It is used for this sort of situation, where you want to keep something that runs per frame consistent based on the framerate. Usually, you multiple the unit you are increasing by this deltaTime.

An implementation could look like this:

local RunService = game:GetService("RunService")

local units = 0
local unitsPerSecond = 10

local function updateUnits(deltaTime)
    -- Here we use * deltaTime, to scale this based on framerate
    units += unitsPerSecond * deltaTime
    print(units)
end

RunService:BindToRenderStep("Example", 251, updateUnits)
4 Likes

Thanks but I already knew this and implemented it the same way you did in your example but it resulted in a stationary camera that would occasionally make small, buggy jumps for a frame and then go back to being stationary.

I was trying this and several variations of it and couldn’t get it to work properly:

local R01 = INTENSITY * math.cos(math.rad(DEGREE)) * delta
local R10 = INTENSITY * math.sin(math.rad(DEGREE)) * delta
1 Like

In this case, I am going to assume that you calibrated the code without the * delta, so when adding it, it drastically lowered the effect. I changed the INTENSITY variable to 2, and got a much more nauseous effect. Try it out.

1 Like

ah right. I tried with higher intensity and was able to get the effect again.

However, now there are more problems lol. The first problem is that the speed of the effect is still different across different framerates. The second is that there is this weirdly intense shake applied on top of it.

Here are videos of the problems:

This is in Roblox Studio (locked to 60 fps of course):
robloxapp-20231210-1403596.wmv (1.1 MB)

This is in the actual game (was hitting around 400 - 500 fps):
robloxapp-20231210-1403064.wmv (1.2 MB)

both of those were with delta time

I am not exactly sure why, but I have seen something similar before. My initial thought is that when the deltaTime is incredibly small, it causes issues with the camera due to the tiny increment per frame? If you change deltaTime to a static value, such as 1/60, the effect looks fine in studio and looks somewhat fine in-game.

In-game, I have ~450-500FPS on an empty baseplate, and it does the exact same effect, just a little faster. I am not sure if there is going to be a pleasant workaround, or if my knowledge on the topic is limited. It seems like just doing * deltaTime should have been enough.

To be fair, 99% of your players will be operating at 120FPS or less, which, with the current function, should look fine.

The videos below use a static delta of 1/60, and are labelled based on the frame-rate:

Effect at uncapped FPS:

Effect capped at 120 FPS:

Effect at standard 60 FPS:

2 Likes

yea ig ur right about the 120 fps or less LOL

thanks for the help tho I’ll use static delta time then

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.