Glitchy camera that follows player movement

I am trying to make a camera that follows where the player is in a room. It stays in one spot and only rotates, which is what I want. The problem is that it occasionally becomes jittery, which is not what I want.

Here is the code:

local runService = game:GetService("RunService")
local tweenService = game:GetService("TweenService")

local player = game:GetService("Players").LocalPlayer
local character = player.CharacterAdded:Wait()

wait(5)
cam = workspace.CurrentCamera

cam.CameraType = "Scriptable"
cam.FieldOfView = 50

local roomCamera = game.Workspace.RoomOneCamera

local info = TweenInfo.new(0.05, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0)


runService.Heartbeat:Connect(function()
	cam.CFrame = roomCamera.CFrame
	local tween = tweenService:Create(roomCamera, info, {CFrame = CFrame.new(roomCamera.Position, character:FindFirstChild("HumanoidRootPart").Position)})
	tween:Play()
end)

I’m not entirely sure if there is a topic that already covers this, but when I looked, the ones I found were either mostly unrelated or didn’t offer any advice for this kind of situation.

The jittery appearance comes from the fact that you’re moving the camera on Heartbeat, which on some frames can happen long after the scene is rendered. Instead of using Heartbeat you’ll want to utilize BindToRenderStep with the Enum.RenderPriority.Camera priority. This should fix the jittering you’re seeing.

1 Like

Thanks, I’ll try it.

(don’t mind these extra words)

It’s still kind of jittery. I’m probably doing something wrong.

local runService = game:GetService("RunService")
local tweenService = game:GetService("TweenService")

local player = game:GetService("Players").LocalPlayer
local character = player.CharacterAdded:Wait()

wait(5)

cam = workspace.CurrentCamera

cam.CameraType = "Scriptable"
cam.FieldOfView = 50

local roomCamera = game.Workspace.RoomOneCamera

local info = TweenInfo.new(0.05, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0)


local function cameraTween()
	cam.CFrame = roomCamera.CFrame
	local tween = tweenService:Create(roomCamera, info, {CFrame = CFrame.new(roomCamera.Position, character:FindFirstChild("HumanoidRootPart").Position)})
	tween:Play()
end

runService:BindToRenderStep("cameraBinding", Enum.RenderPriority.Camera.Value, cameraTween)

Should it be rendered before or after?

Nvm, it works. It works while on an actual roblox server instead of in roblox studio.

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