Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Weird Camera Shake? [UNANSWERED]

Asked by
LevelKap 114
8 years ago

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)
0
Sorry about the weird cutting, it has something to do with copy and pasting. LevelKap 114 — 8y
0
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. koolkid8099 705 — 8y
0
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? LevelKap 114 — 8y
0
Could you do me a favour and mark my answer as accepted? :) DevSean 270 — 8y

1 answer

Log in to vote
1
Answered by
DevSean 270 Moderation Voter
8 years ago

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)

0
Thank you so much! I'm not very familiar with camera manipulation, so thanks again LevelKap 114 — 8y
Ad

Answer this question