Choppy camera movement

I made a simple script to handle camera swaying for first person (breathing, walking, running.) My issue is when you go from walking to idling, it snaps into position.

Video:

Code:

local RunService = game:GetService("RunService")

local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humrp = char:WaitForChild("HumanoidRootPart")
local hum = char:WaitForChild("Humanoid")

local Cam = workspace.CurrentCamera

player.CameraMode = Enum.CameraMode.LockFirstPerson

Cam.FieldOfView = 90

hum.CameraOffset = Vector3.new(0, 0.25, -1)


RunService.RenderStepped:Connect(function()
	local sin = math.sin(time() * 10) / 4
	local cos = math.cos(time() * 5) / 2
	
	local offset : Vector3 = Vector3.new(0,0,0)
	
	if hum.MoveDirection.Magnitude > 0 then
		if hum.WalkSpeed <= 16 then
			offset = Vector3.new(cos, sin, -1)
		elseif hum.WalkSpeed > 16 then
			local sin = math.sin(time() * 12) / 2
			local cos = math.cos(time() * 6) / 4
			
			offset = Vector3.new(cos * 2, sin, -1)
		end
	else
		local sin = math.sin(time() * 3) / 6
		local cos = math.cos(time() * 1) / 6
		offset = Vector3.new(cos, sin, -1)
	end
	
	hum.CameraOffset = Vector3.new(0, 1, 0) + offset
	
end)

Hello, you could potentially smoothen things out using linear interpolation based on delta provided in your renderStepped function. What this does is essentially moving the current position to the actual target position based on a ratio, meaning that the further something is, the faster it will move to the target and vice versa. This allows for smooth quadratic/cubic-like interpolation at not much of a cost in-terms of calculations, and has found many uses especially in fast camera interpolation.

Here’s what it would look like:

-- outside of RenderStepped:
local lerpFactor = 6 -- higher values = faster interpolation

-- (this is replacing your third to last line in your code you sent)
local targetOffset = Vector3.new(0,1,0)
hum.CameraOffset = hum.CameraOffset:Lerp(targetOffset,delta*lerpFactor)

Note: delta is the parameter given by the RenderStepped function on callback. (RunService.RenderStepped:Connect(function(delta))

Hope this helps!

Thank you so much, I don’t have much experience with the lerp function so this helps a lot!

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