Any way to make camera follow it's target without using while loop?

So what this code does is basically uses a tween to move the camera up and down continuously. It works just fine but if I make the part visible, it seems to sort of ‘z-fight’ with the camera, as shown in the video.

--Camera Variables
local target = game.Workspace.Part
local camera = game.Workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable 
camera.CameraSubject = target

--Tweening Stuff
local ts = game:GetService("TweenService")
local part = game.Workspace.Part 
local info = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, -1, true, 0)
local goals = {Position = Vector3.new(-39, 18, -4)}
local goals2 = {Position = Vector3.new(-39, 36, -20)}
local tween = ts:Create(part, info, goals)
tween:Play()

--camera thingy

while wait() do
	camera.CoordinateFrame = CFrame.new(target.Position)
end

Is there a better way of going about with this or is this normal no matter how I do it?

(This is in a LocalScript in StarterPlayerScripts)

Just use Camera.CameraSubject, it will follow the thing you want.

1 Like

No idea why you doing this!
Maybe this helps:
CameraType = ‘Track’
CameraSubject = ‘target’
And lock player camera in first person!
Try that

1 Like

This did not work. I set the Type to ‘Track’ and locked the camera in first person, also set camerasubject to target but it focused on the player instead of the target.

Well your trying to play a tween and reposition the camera position at the same time. That’s why it’s fighting.
Sidenote: camera.CoordinateFrame shouldn’t be used, camera.CFrame should.

The “z-fighting” you experience is due to the camera not being able to keep up with the part; the part is rendered at 60fps, while a wait() loop only runs (at best) at 30fps. Simply change the loop to a RenderStepped connection.

game:GetService("RunService").RenderStepped:connect(function()
	camera.CoordinateFrame = CFrame.new(target.Position)
end)
3 Likes