I made a 2D camera script, which basically follows the player when the player gets outside a certain range of the camera. For some reason I have a weird camera shake when i'm not moving, however when i move, its completely fine. If i remove the line of code that moves the camera left, I stop shaking, but then naturally, the camera will only move when i'm moving to the right. Any help is appreciated!
Here's the code:
-- Camera setup
local cameraHeight = 12
local cameraZOffset = 20
local cameraXChase = 10
local cameraSpeed = 0.25
local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local RunService =game:GetService("RunService")
local function setupCamera()
camera.CoordinateFrame = CFrame.new(Vector3.new(0,cameraHeight,cameraZOffset),Vector3.new(0,
cameraHeight,0))
end
setupCamera()
player.CharacterAdded:connect(setupCamera)
local function onUpdate()
if player.Character and player.Character:FindFirstChild("Torso") then
local playerX = player.Character.Torso.Position.X
local cameraX = camera.CoordinateFrame.p.X
if cameraX - cameraXChase < playerX then
camera.CoordinateFrame = camera.CoordinateFrame + Vector3.new(cameraSpeed, 0, 0) -- Moves camera right
elseif cameraX - cameraXChase > playerX then
camera.CoordinateFrame = camera.CoordinateFrame - Vector3.new(cameraSpeed, 0, 0) -- Moves camera left
end
end
end
RunService:BindToRenderStep("Camera",Enum.RenderPriority.Camera.Value, onUpdate)

4 comments
Sorry about the weird cutting, it has something to do with copy and pasting.
I did not test it out yet so this may not be true, but maybe it shakes because the camera is trying to go left while going right at the same time.
I tried changing the code so that it checks whether the camera is moving right, if it isn't and the player moves too far left, the camera will move. However it seems to be bugging out again. Any suggestions or implementations?
Could you do me a favour and mark my answer as accepted? :)