My camera sway is working but camera is like jittery?

so i got the camera sway working but it’s jittery

im using a renderstepped loop so im not sure why its happening

here is the code

local currentTilt = 0
local tiltSpeed = 10

local function lerp(a, b, t)
    return a + (b - a) * t
end


game:GetService("RunService").RenderStepped:Connect(function(deltaTime)
  local targetTilt = sway.Amount
  
  currentTilt = lerp(currentTilt, targetTilt, deltaTime * tiltSpeed)
  
  local currentCFrame = camera.CFrame
  local x, y, z = currentCFrame:ToEulerAnglesYXZ()
  
  local newCFrame = CFrame.new(currentCFrame.Position) * CFrame.fromEulerAnglesYXZ(x, y, z + (currentTilt * deltaTime))
  camera.CFrame = newCFrame
end)
1 Like

Try changing your tiltSpeed to .5 or 100 to see what difference that makes.

Alright I tried this but cranking it up made the effect greater and more jittery and making it lower made the effect barely noticable but stopping the jitter

What does sway point to? Is it a table?

yeah so the sway is actually the amount that the camera has moved the previous frame in another module, i wasnt sure if i needed too add it since it wasnt exactly the part that wasnt working but here it is

local sway = {}
sway.Amount = 0

local camera = workspace.CurrentCamera

local previousCameraRotation = nil
local swayFactor = 1000

game:GetService("RunService").RenderStepped:Connect(function(deltaTime)
   local currentCameraRotation = camera.CFrame
   if previousCameraRotation then
      local difference = previousCameraRotation:Inverse() * currentCameraRotation
      local x,y,z = difference:ToEulerAnglesXYZ()
      local swayValue = -y

      sway.Amount = 1 - (swayValue * swayFactor)
   end

   previousCameraRotation = currentCameraRotation
end)

return sway

Did you try using a Stepped event rather than a RenderStepped event?

No but let me try that right now, but I have a feeling it has something to do with setting the previousCameraRotation to the currentCameraRotation and then in the main CameraTilt im setting the cframe again but im not sure

I’ve had a similar issue before and I found a thread explaining about the order of exection to fix lerp jittering, but sadly I lost it. Changing RenderStepped to Stepped fixed my issue.

Hm interesting, I changed the loops to stepped but that didnt work, I just tried combining both scripts but the jittering is still happening

In this script, deltaTime is acounted for twice, which is probably a mistake.

currentTilt = lerp(currentTilt, targetTilt, deltaTime * tiltSpeed)

local newCFrame = CFrame.new(currentCFrame.Position) * CFrame.fromEulerAnglesYXZ(x, y, z + (currentTilt * deltaTime))

So just get rid of one or the other.
(and mabye both if that dosent solve the issue)

Yeah that jst made the camera spin uncontrollablly, I even made the camera scriptable and create my own custom camera controller and it’s still happening

local UserInputService = game:GetService("UserInputService")
local cameraController = {}

local player = game.Players.LocalPlayer
local character = player.Character
local humanoidrootpart = character.HumanoidRootPart

local camera = workspace.CurrentCamera
local sens = 1
local distance = 8
local maxAngleY = 55

local previousCameraCFrame = nil
local factor = 800
local sway = 0

local currentAngleX, currentAngleY = 0,0

for i = 1,10 do
   camera.CameraType = Enum.CameraType.Scriptable
end

local function Lerp(a,b,t)
   return a + (b - a) * t
end

game:GetService("RunService").RenderStepped:Connect(function(dt)

   local currentCameraCFrame = camera.CFrame

   if previousCameraCFrame then
      local difference = previousCameraCFrame:Inverse() * currentCameraCFrame
      local _,y,_ = difference:ToEulerAnglesXYZ()
      sway = Lerp(sway, 1 - (y * factor), dt * 10)
   end

   if UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton2) then
       
      UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
       local delta = UserInputService:GetMouseDelta()

       currentAngleX = currentAngleX + delta.X * sens * dt
       currentAngleY = math.clamp(currentAngleY + delta.Y * sens * dt,-math.rad(maxAngleY), math.rad(maxAngleY))

   else
       UserInputService.MouseBehavior = Enum.MouseBehavior.Default
   end

   camera.CFrame = CFrame.new(humanoidrootpart.CFrame.Position) * CFrame.fromEulerAnglesYXZ(-currentAngleY, -currentAngleX,sway * dt) * CFrame.new(0,1, distance)
   previousCameraCFrame = currentCameraCFrame
end)


return cameraController

Alright so I FINALLY figured it out, so bascially either tweening the camera’s cframe or using lerp and it works greattt!!!

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