How to make camera shake the same distance from the starting point?

I am trying to make a camera follow a npc in an infinite corridor, and I wanted to add some camera shaking to look like you’re walking, but I’ve came across this problem wich makes the camera turn to one side, but instead of going to the other side, it goes back into the start position.

This was the script:

local player = game.Players.LocalPlayer
local cam = game.Workspace.CurrentCamera
local run = game:GetService("RunService")
local is = ""
local Waifu = game.Workspace.ZeroTwo
script.Parent.MouseButton1Click:Connect(function()
	cam.CameraType = Enum.CameraType.Scriptable
	cam.CFrame = camPart.CFrame
	script.Parent.Parent.Parent.Enabled = false
	is = false
end)

local leftOrRight = false
local count = 25
run.RenderStepped:Connect(function(delta)
	wait()
	if not is then
		local corridor = game.Workspace.Corridor:Clone()
		corridor:SetPrimaryPartCFrame(corridor.PrimaryPart.CFrame * CFrame.new(0,0,-1))
		game.Workspace.Corridor:Destroy()
		corridor.Parent = game.Workspace
		cam.CFrame = cam.CFrame * CFrame.new(0,0,-1)
		Waifu.PrimaryPart.CFrame = Waifu.PrimaryPart.CFrame * CFrame.new(0,0,-1)
		print("s")
		is = true
	elseif leftOrRight == false and is ~= "" then
		local corridor = game.Workspace.Corridor:Clone()
		corridor:SetPrimaryPartCFrame(corridor.PrimaryPart.CFrame * CFrame.new(0,0,-1))
		game.Workspace.Corridor:Destroy()
		corridor.Parent = game.Workspace
		cam.CFrame = cam.CFrame * CFrame.new(0,0,-1) * CFrame.Angles(0,0,math.rad(-1))
		Waifu.PrimaryPart.CFrame = Waifu.PrimaryPart.CFrame * CFrame.new(0,0,-1)
		count = count - 1
		if count == 0 then
			leftOrRight = true
			count = 25
		end	
	elseif leftOrRight == true and is ~= "" then
		local corridor = game.Workspace.Corridor:Clone()
		corridor:SetPrimaryPartCFrame(corridor.PrimaryPart.CFrame * CFrame.new(0,0,-1))
		game.Workspace.Corridor:Destroy()
		corridor.Parent = game.Workspace
		cam.CFrame = cam.CFrame * CFrame.new(0,0,-1) * CFrame.Angles(0,0,math.rad(1))
		Waifu.PrimaryPart.CFrame = Waifu.PrimaryPart.CFrame * CFrame.new(0,0,-1)
		count = count - 1
		if count == 0 then
			leftOrRight = false
			count = 25
		end	
	end
end)

I tried to fix that by doubling the angles into the other direction, but somehow it ended up like this:

This is the script:

local player = game.Players.LocalPlayer
local cam = game.Workspace.CurrentCamera
local run = game:GetService("RunService")
local is = ""
local Waifu = game.Workspace.ZeroTwo
script.Parent.MouseButton1Click:Connect(function()
	cam.CameraType = Enum.CameraType.Scriptable
	cam.CFrame = camPart.CFrame
	script.Parent.Parent.Parent.Enabled = false
	is = false
end)

local leftOrRight = false
local count = 25
run.RenderStepped:Connect(function(delta)
	wait()
	if not is then
		local corridor = game.Workspace.Corridor:Clone()
		corridor:SetPrimaryPartCFrame(corridor.PrimaryPart.CFrame * CFrame.new(0,0,-1))
		game.Workspace.Corridor:Destroy()
		corridor.Parent = game.Workspace
		cam.CFrame = cam.CFrame * CFrame.new(0,0,-1)
		Waifu.PrimaryPart.CFrame = Waifu.PrimaryPart.CFrame * CFrame.new(0,0,-1)
		print("s")
		is = true
	elseif leftOrRight == false and is ~= "" then
		local corridor = game.Workspace.Corridor:Clone()
		corridor:SetPrimaryPartCFrame(corridor.PrimaryPart.CFrame * CFrame.new(0,0,-1))
		game.Workspace.Corridor:Destroy()
		corridor.Parent = game.Workspace
		cam.CFrame = cam.CFrame * CFrame.new(0,0,-1) * CFrame.Angles(0,0,math.rad(-1))
		Waifu.PrimaryPart.CFrame = Waifu.PrimaryPart.CFrame * CFrame.new(0,0,-1)
		count = count - 1
		if count == 0 then
			leftOrRight = true
			count = 25
		end	
	elseif leftOrRight == true and is ~= "" then
		local corridor = game.Workspace.Corridor:Clone()
		corridor:SetPrimaryPartCFrame(corridor.PrimaryPart.CFrame * CFrame.new(0,0,-1))
		game.Workspace.Corridor:Destroy()
		corridor.Parent = game.Workspace
		cam.CFrame = cam.CFrame * CFrame.new(0,0,-1) * CFrame.Angles(0,0,math.rad(2)) -- changed from 1 to 2
		Waifu.PrimaryPart.CFrame = Waifu.PrimaryPart.CFrame * CFrame.new(0,0,-1)
		count = count - 1
		if count == 0 then
			leftOrRight = false
			count = 25
		end	
	end
end)

Thanks in advance!
Edit: messed up the code block, it’s fixed now.

2 Likes

Your problem is that it counts down from 25 to the right, and then counts down from 25 to the center. To go all the way to the left, you need another 25.
So you need to program it so that it returns to the center before switching leftOrRight. Or, for an even easier fix, change count = 25 to count = 50 on the end of the functions, but leave it at 25 at the beginning. I don’t know how that will behave if you ever stop and restart the shaking though.

4 Likes

@JarodOfOrbiter I did that, and it does the same thing as changing from math.rad(1) to math.rad(2) but in the opposite direction?

1 Like

Did you get both count = 50 set? If you only do one, you will get that result. There are three, and only the first should still say count = 25

1 Like

Yeah, I only set for the second elseif. It’s going in both directions now. Thank you!

1 Like