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)
Let me explain what's wrong.
Your problem lies in the following lines:
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
The first line checks if the camera is left of where you want it, the second line checks if the camera is right of where you want it, one of these will always return true.
When it reaches the closest point the camera will move back and forth as it changes to the left and right of where you want it.
To fix this, you should check the cameras position + the move speed like so:
local cameraX = camera.CoordinateFrame.p.X - cameraXChase -- simplified this variable since you - cameraXChase always if cameraX + cameraSpeed < playerX then camera.CFrame = camera.CFrame + Vector3.new(cameraSpeed, 0, 0) -- Moves camera right elseif cameraX - cameraSpeed > playerX then camera.CFrame = camera.CFrame - Vector3.new(cameraSpeed, 0, 0) -- Moves camera left else -- move to the char end
This stops the jittering effect.
Lastly we should move the camera to the ideal location like so:
camera.CFrame = camera.CFrame + Vector3.new(playerX - cameraX, 0, 0) -- minus cameraX so it's moved to -- the ideal position for -- the player
The full edited code is listed below:
-- Camera setup local cameraHeight = 12 local cameraZOffset = 20 local cameraXChase = 10 local cameraSpeed = 0.25 local camera = game.Workspace.CurrentCamera camera.CameraType = Enum.CameraType.Scriptable -- Is this necessary for you? local player = game.Players.LocalPlayer local RunService =game:GetService("RunService") local function setupCamera() camera.CFrame = 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 - cameraXChase if cameraX + cameraSpeed < playerX then camera.CFrame = camera.CFrame + Vector3.new(cameraSpeed, 0, 0) -- Moves camera right elseif cameraX - cameraSpeed > playerX then camera.CFrame = camera.CFrame - Vector3.new(cameraSpeed, 0, 0) -- Moves camera left else camera.CFrame = camera.CFrame + Vector3.new(playerX - cameraX, 0, 0) end end end RunService:BindToRenderStep("Camera",Enum.RenderPriority.Camera.Value, onUpdate)