Need help to make this camera smoother

Hi if you are reading this, helping out would be great! In the video below, the camera works, however, the camera’s movement is a bit laggy and its very annoying. Is there a way to make the camera smoother?

Here is the code I used:

local camera = script.Parent

function giveCamera (player)
 if player then
  local character = player.Character
  if character then
   local humRootPart = character.HumanoidRootPart
   
   local bodyPos = Instance.new("BodyPosition", camera)
   bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
   
   local bodyGyro = Instance.new("BodyGyro", camera)
   bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
   
   while wait() do
    bodyPos.Position = humRootPart.Position + Vector3.new(2, 2, 3)
    bodyGyro.CFrame = humRootPart.CFrame
   end
  end
 end
end

game.Players.PlayerAdded:Connect(function(player)
 player.CharacterAdded:Connect(function(char)
  giveCamera(player)
 end)
end)

wait() doesn’t wait for 0 seconds, it waits for approximately 0.033 seconds, or 1/30th of a second. You can test exactly how long it takes (it varies) by pasting this to the command bar in Studio:

local t0 = tick(); wait(); local t = tick(); print(t - t0)

If your PC is good enough, Roblox can run at up to 60 fps, so if you update every wait(), you’ll be updating at half the maximum fps. The easiest alternative to wait() that runs at 60 hz is RunService.RenderStepped. So you can change the while loop to this:

while RunService.RenderStepped:Wait() do

Just make sure you define RunService at the top of the script.

1 Like

It works however, the camera is now not following the player. Instead of the humanoid root part its going to a random spot near the middle of the baseplate.

The inconsistency in the camera’s movement may be because you’re applying a force to the camera to move it around, rather than adjusting the CFrame/Position of the camera relative to the character.

For smoothness of movement, consider using tweening.

1 Like