Skip to content

Instantly share code, notes, and snippets.

@cipharius
Last active February 9, 2020 06:46
Show Gist options
  • Save cipharius/72db41a70a63ad18ab8b1c8c44bfe1a6 to your computer and use it in GitHub Desktop.
Save cipharius/72db41a70a63ad18ab8b1c8c44bfe1a6 to your computer and use it in GitHub Desktop.
Roblox random camera sway
local camera = workspace.CurrentCamera
local fixedCFrame = CFrame.new(0, 100, 0) -- CFrame of default looking direction/position
local rate = 1/5 -- Speed at which the camera will sway
local xSwayAngle = math.pi/9 -- X angle change interval
local ySwayAngle = math.pi/9 -- Y angle change interval
local rollAngle = math.pi/18 -- Roll angle change interval
function update(t)
local phi = math.pi/2 + math.noise(t*rate, 0) * xSwayAngle
local theta = math.pi/2 + math.noise(t*rate, 10) * ySwayAngle
local lookDir = Vector3.new(math.sin(theta)*math.cos(phi), math.cos(theta), math.sin(theta)*math.sin(phi))
local backVec = -fixedCFrame:VectorToWorldSpace(lookDir)
local upVec = Vector3.new(0, 1, 0)
local rightVec = backVec:Cross(upVec)
local newCF = CFrame.fromMatrix(fixedCFrame.p, rightVec, rightVec:Cross(backVec))
camera.CFrame = newCF * CFrame.fromAxisAngle(backVec, math.noise(t*rate, 20) * rollAngle)
end
local t = 0
game:GetService("RunService"):BindToRenderStep("random-camera-sway", Enum.RenderPriority.Camera.Value, function(dt)
t = t + dt
update(t)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment