Player camera not moving consistently on different fps

  1. What do you want to achieve?
    I need to make the player camera move consistently when walking

  2. What is the issue?
    I have made camera movement function that basically just moves the camera around. If I run the game at 60 fps, everything seems fine. However, if I run it at 240 fps, then the movement will become more intense. I’m pretty sure to solve this problem, I’ll need to use delta time but I don’t know how to implement it in my code.

  3. What solutions have you tried so far?
    As I mentioned earlier, I have tried using delta time to multiply my values but they have all ended in weird results. I have also looked everywhere but I cant seem to find any answers online. Here is a video describing the issue/

Here is the code. The function is being run in a RenderStepped event.

local function SwayViewModel(deltaTime : number)
	local tick = tick()
	local cameraCFrame : CFrame = CFrame.new()
	local moveX, moveY, moveZ = 0, 0, 0

	if IsPlayerMoving() then
		moveX = viewModelSwaySettings.amplitudeX * math.cos(tick * viewModelSwaySettings.speedX * math.pi)
		moveY = viewModelSwaySettings.amplitudeY * math.sin(tick * viewModelSwaySettings.speedY * math.pi * 2)
		moveZ = viewModelSwaySettings.amplitudeZ * math.cos(tick * 0.5 * math.pi * 4)
		
		cameraCFrame = CFrame.new(moveX * 5, moveY * 5, moveZ * 2, math.rad(moveZ), 0, math.rad(moveX * 5), 1)
	end
	camera.CFrame = camera.CFrame:Lerp(camera.CFrame * cameraCFrame, 1)	
end
1 Like

RunService runs on how fast the fps is going in seconds, as you stated in the parameter (Delta) is how much milliseconds it has reached the next frame so we would have to use it in the alpha.

If we wanted the fps then we would want to do
local fps = (1 / deltatime) to get fps

You could try to clamp it like:

alphatime = math.clamp(1 / fps, 0, 1)

If I remember you would have to divide the alpha with the current fps

Maybe like:

CFrame:Lerp(CFramepos, alpha / fps)

or

CFrame:Lerp(CFramepos, alphatime)

Otherwise this is how I did it with my camera system, I hope this helps or at least got an idea to mess around with!

Also another note you also could divide the number value like the x axis with fps variable too