Here is my game camera code:
local camclass = {}
local Character = game.Players.LocalPlayer.Character
local Player = game.Players.LocalPlayer
local cam = workspace.CurrentCamera
local runService = game:GetService("RunService")
local transparencyConnections = {}
camclass.__index = camclass
function camclass.create(tiltspeedZ,bobbingspeed)
local camobj = {}
local humrootpart = Character:WaitForChild("HumanoidRootPart")
local hum:Humanoid = Character:WaitForChild("Humanoid")
local tiltSpeedZ = tiltspeedZ
local bobbingSpeed = bobbingspeed
local camclipfix:RBXScriptConnection = nil
local cambobconnect = nil
cam.CameraSubject = Character.Humanoid
cam.CFrame = Character.PrimaryPart.CFrame
Player.CameraMode = Enum.CameraMode.LockFirstPerson
cam.CameraType = Enum.CameraType.Custom
--onchartrans()
local lastcamcf = nil
local tweenplaying = false
--smoothcam
--smoothcamend
camclipfix = runService.RenderStepped:Connect(function()
local ignore = RaycastParams.new()
ignore.FilterType = Enum.RaycastFilterType.Exclude
ignore.FilterDescendantsInstances = Character:GetChildren()
local camray = workspace:Raycast(Character.char.Position, ((Character.char.CFrame + Character.char.CFrame.LookVector * 2) - Character.char.Position).Position.Unit)
if camray then
Character.Humanoid.CameraOffset = Vector3.new(0, 1, - (Character.char.Position - camray.Position).Magnitude)
else
hum.CameraOffset = Vector3.new(0,-0.7,-0.9)
end
end)
local tilt = 0
local sinValue = 0
local function lerp(a, b, t)
task.desynchronize()
local value = a + (b - a) * t
task.synchronize()
return value
end
local function calculateSine(speed, intensity)
task.desynchronize()
sinValue += speed
if sinValue > (math.pi * 2) then sinValue = 0 end
local sineY = intensity * math.sin(2 * sinValue)
local sineX = intensity * math.sin(sinValue)
local sineCFrame = CFrame.new(sineX, sineY, 0)
task.synchronize()
return sineCFrame
end
local previousSineX = 0
local previousSineY = 0
cambobconnect = runService.RenderStepped:Connect(function(dt)
local movementVector = cam.CFrame:vectorToObjectSpace(humrootpart.Velocity / math.max(hum.WalkSpeed, 0.01))
local speedModifier = (hum.WalkSpeed / 16)
tilt = math.clamp(lerp(tilt, movementVector.X * tiltSpeedZ, 0.1), -0.05, 0.1) / 2
local sineCFrame = calculateSine(bobbingSpeed * speedModifier, movementVector.Z * speedModifier)
local lerpedSineX = lerp(previousSineX, sineCFrame.X, 0.1)/2
local lerpedSineY = lerp(previousSineY, sineCFrame.Y, 0.1)
cam.CFrame *= CFrame.Angles(0, 0, tilt) * CFrame.new(lerpedSineX, lerpedSineY, 0)
previousSineX = lerpedSineX
previousSineY = lerpedSineY
end)
camobj.bobbingspeed = bobbingspeed
camobj.tiltingspeed = tiltspeedZ
camobj.cambobConnection = cambobconnect
camobj.camclipConnection = camclipfix
camobj.running = true
setmetatable(camobj,camclass)
return camobj
end
function camclass:enablechar()
Character.char.Transparency = 0
for i,v in pairs(Character:GetDescendants()) do
if v:IsA("BasePart") then
table.insert(transparencyConnections,v:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
v.LocalTransparencyModifier = v.Transparency
end))
end
end
end
function camclass:disablechar()
for i,v in pairs(transparencyConnections) do
v:Disconnect()
end
Character.char.Transparency = 1
end
function camclass:pause()
self.running = false
cam.CameraType = Enum.CameraType.Scriptable
self.cambobConnection:Disconnect()
self.camclipConnection:Disconnect()
end
function camclass:resume()
if self.running == false then
local humrootpart = Character:WaitForChild("HumanoidRootPart")
local hum:Humanoid = Character:WaitForChild("Humanoid")
local tiltSpeedZ = self.tiltingspeed
local bobbingSpeed = self.bobbingspeed
local camclipfix:RBXScriptConnection = nil
local cambobconnect = nil
cam.CameraSubject = Character.Humanoid
cam.CFrame = Character.PrimaryPart.CFrame
Player.CameraMode = Enum.CameraMode.LockFirstPerson
cam.CameraType = Enum.CameraType.Custom
--onchartrans()
local lastcamcf = nil
local tweenplaying = false
--smoothcam
--smoothcamend
camclipfix = runService.RenderStepped:Connect(function()
local ignore = RaycastParams.new()
ignore.FilterType = Enum.RaycastFilterType.Exclude
ignore.FilterDescendantsInstances = Character:GetChildren()
local camray = workspace:Raycast(Character.char.Position, ((Character.char.CFrame + Character.char.CFrame.LookVector * 2) - Character.char.Position).Position.Unit)
if camray then
Character.Humanoid.CameraOffset = Vector3.new(0, 1, - (Character.char.Position - camray.Position).Magnitude)
else
hum.CameraOffset = Vector3.new(0,-0.7,-0.9)
end
end)
local tilt = 0
local sinValue = 0
local function lerp(a, b, t)
task.desynchronize()
local value = a + (b - a) * t
task.synchronize()
return value
end
local function calculateSine(speed, intensity)
task.desynchronize()
sinValue += speed
if sinValue > (math.pi * 2) then sinValue = 0 end
local sineY = intensity * math.sin(2 * sinValue)
local sineX = intensity * math.sin(sinValue)
local sineCFrame = CFrame.new(sineX, sineY, 0)
task.synchronize()
return sineCFrame
end
local previousSineX = 0
local previousSineY = 0
cambobconnect = runService.RenderStepped:Connect(function(dt)
local movementVector = cam.CFrame:vectorToObjectSpace(humrootpart.Velocity / math.max(hum.WalkSpeed, 0.01))
local speedModifier = (hum.WalkSpeed / 16)
tilt = math.clamp(lerp(tilt, movementVector.X * tiltSpeedZ, 0.1), -0.05, 0.1) / 2
local sineCFrame = calculateSine(bobbingSpeed * speedModifier, movementVector.Z * speedModifier)
local lerpedSineX = lerp(previousSineX, sineCFrame.X, 0.1)/2
local lerpedSineY = lerp(previousSineY, sineCFrame.Y, 0.1)
cam.CFrame *= CFrame.Angles(0, 0, tilt) * CFrame.new(lerpedSineX, lerpedSineY, 0)
previousSineX = lerpedSineX
previousSineY = lerpedSineY
end)
self.cambobConnection = cambobconnect
self.camclipConnection = camclipfix
self.running = true
end
end
return camclass
Is that enough info?