Wow! Well that is an entire essay! Jokes aside! I read it all and my knowledge has increased significantly! Thank you for telling me about data compression and how roblox manages variables. Now I can safely add 1000 variables in my code!
Code for a camera system.
It is actually for a camera system I am currently making. It is still not complete btw.
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = script.Parent
local HumanoidRootPart:BasePart = Character:WaitForChild("HumanoidRootPart")
local Head:BasePart = Character:WaitForChild("Head")
local Torso:BasePart = Character:WaitForChild("Torso")
local Humanoid = Character:WaitForChild("Humanoid")
local Camera = workspace.CurrentCamera
local Shake = script.ShakeIntensity
local FOV = script.FOV
local Offset = script.CameraOffset
local Move = Character.Move
local canMove = Move.Movement
local currentAction = Move.CurrentAction
local RootJoint:Motor6D = Character:FindFirstChild("RootJoint",true)
local Neck:Motor6D = Character:FindFirstChild("Neck",true)
local LeftHip:Motor6D = Character:FindFirstChild("Left Hip",true)
local RightHip:Motor6D = Character:FindFirstChild("Right Hip",true)
local LeftShoulder:Motor6D = Character:FindFirstChild("Left Shoulder",true)
local RightShoulder:Motor6D = Character:FindFirstChild("Right Shoulder",true)
local RootJointC0 = RootJoint.C0
local NeckC0 = Neck.C0
local LeftHipC0 = LeftHip.C0
local RightHipC0 = RightHip.C0
local LeftShoulderC0 = LeftShoulder.C0
local RightShoulderC0 = RightShoulder.C0
local cameraRotation = Vector2.new(0,0)
local cameraDistance = 0
local deltaTime = 1/60
local old_finalRotation = CFrame.Angles(0,0,0)
local old_cameraOffset = CFrame.new(0,0,0)
local old_shakeCFrame = CFrame.new(0,0,0)
local oldX = 0
local currentShoulder = 1
local leaning = false
local old_bobbleX,old_bobbleY = 0,0
local whitelistedParts = {"Left Arm","Right Arm","Left Leg","Right Leg"}
local holdStart = 0
local tiltX,tiltY = 0,0
local rayCast_Params = RaycastParams.new()
rayCast_Params.FilterDescendantsInstances = {Character}
Camera.CameraType = Enum.CameraType.Scriptable
local function InFirstPerson()
return cameraDistance <= 0
end
local function GetMovementVector()
local HRPCFrame = HumanoidRootPart.CFrame
local HRPVelocity = HumanoidRootPart.AssemblyLinearVelocity
local movementVector = Vector3.zero
if HRPVelocity.Magnitude > 1 then
movementVector = HRPCFrame:VectorToObjectSpace(Vector3.new(HRPVelocity.X,0,HRPVelocity.Z)).Unit
end
movementVector = Vector3.new(math.round(movementVector.X),0,math.round(movementVector.Z))
return movementVector
end
local function Lerp(a,b,t)
return a+(b-a)*t
end
local function MakeInvisible(Status)
for _,part in pairs(Character:GetChildren()) do
if part:IsA("BasePart") and not table.find(whitelistedParts,part.Name) then
part.LocalTransparencyModifier = Status and 1 or 0
elseif part:IsA("Accessory") then
part.Handle.LocalTransparencyModifier = Status and 1 or 0
elseif part:IsA("Decal") then
part.LocalTransparencyModifier = Status and 1 or 0
end
end
end
local function ToolEquipped()
return Character:FindFirstChildOfClass("Tool")
end
RunService.RenderStepped:Connect(function()
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
local HeadPosition = Head.Position
local HeadCFrame = CFrame.new(HeadPosition)
local HeadXRotation,_,HeadZRotation = Head.CFrame:ToOrientation()
local HRPCFrame = HumanoidRootPart.CFrame
local HRPPosition = HumanoidRootPart.Position
local HRPVelocity = HumanoidRootPart.AssemblyLinearVelocity
local HRPVMagnitude = HRPVelocity.Magnitude
local movementVector = GetMovementVector()
local movingBackwards = movementVector.Z > 0 and -1 or 1
local noiseX = math.noise(tick()/10,os.clock()/10)*5
local noiseY = math.noise(os.clock()/10,tick()/10)*5
local bX = math.sin(time() * HRPVMagnitude/2)*((HRPVMagnitude/16)^(1/3))*2
local bY = math.abs(math.sin(time() * HRPVMagnitude/2))*((HRPVMagnitude/16)^(1/3))*2
local bobbleX = Lerp(old_bobbleX,bX,deltaTime*HRPVMagnitude)
local bobbleY = Lerp(old_bobbleY,bY,deltaTime*HRPVMagnitude)
local xRotation = cameraRotation.X+bobbleX+noiseX+math.deg(HeadXRotation/5)
local yRotation = cameraRotation.Y+bobbleY+noiseY
local zRotation = (oldX-cameraRotation.X)+(GetMovementVector().X*HRPVelocity.Magnitude/8)+bobbleX/2+math.deg(HeadZRotation/5)
local cameraAngle = CFrame.Angles(0,math.rad(xRotation),0)*CFrame.Angles(math.rad(yRotation),0,math.rad(zRotation))
local finalRotation = old_finalRotation:Lerp(cameraAngle,deltaTime*20)
local rayOrigin = HeadPosition
local rayDirection = (HeadCFrame*CFrame.Angles(Camera.CFrame:ToOrientation())).RightVector*currentShoulder*3
local ray = workspace:Raycast(rayOrigin,rayDirection,rayCast_Params)
local sideDistance = ray ~= nil and 0 or math.huge
local offset = CFrame.new(InFirstPerson() and 0 or math.min(2,sideDistance)*currentShoulder,InFirstPerson() and 2/3 or 0,0)
local rayOrigin2 = (HeadCFrame*offset).Position-Camera.CFrame.LookVector
local rayDirection2 = Camera.CFrame.LookVector*-cameraDistance
local ray2 = workspace:Raycast(rayOrigin2,rayDirection2,rayCast_Params)
local final_cameraDistance = ray2 ~= nil and ray2.Distance-0.5 or math.huge
offset *= CFrame.new(currentShoulder*(leaning and InFirstPerson() and 1 or 0),0,math.min(cameraDistance,final_cameraDistance))*CFrame.Angles(0,0,math.rad((leaning and -30 or 0)*currentShoulder))
local cameraOffset = old_cameraOffset:Lerp(offset,deltaTime*10)
local shake_pX,shake_pY,shake_pZ = math.random()-0.5,math.random()-0.5,math.random()-0.5
local shake_oX,shake_oY,shake_oZ = math.rad(math.random()-0.5)*10,math.rad(math.random()-0.5)*10,math.rad(math.random()-0.5)*10
local shakePositional = CFrame.new(Vector3.new(shake_pX,shake_pY,shake_pZ)*Shake.Value)
local shakeOrientational = CFrame.Angles(shake_oX*Shake.Value,shake_oY*Shake.Value,shake_oZ*Shake.Value)
local shakeCFrame = old_shakeCFrame:Lerp(shakePositional*shakeOrientational,deltaTime*10)
Camera.CFrame = HeadCFrame*finalRotation*cameraOffset*Offset.Value*shakeCFrame
Camera.FieldOfView = Lerp(Camera.FieldOfView,FOV.Value+Vector2.new(HRPVelocity.X,HRPVelocity.Z).Magnitude/1.25,deltaTime*20)
local HRProtX,HRProtY,HRProtZ = HRPCFrame:ToOrientation()
local CamerarotX,CamerarotY,_ = Camera.CFrame:ToOrientation()
local TorsorotX,TorsorotY,_ = Torso.CFrame:ToOrientation()
if canMove.Value then
tiltY = Lerp(tiltY, movementVector.X*movingBackwards,deltaTime*5)
tiltX = Lerp(tiltX,movementVector.Z*HRPVMagnitude/16,deltaTime*5)
local torsoTilt_Y = math.clamp(-tiltY*40*(movementVector.Z ~= 0 and 1/(2^(1/2)) or 1.25),-30,30)
RootJoint.C0 = RootJoint.C0:Lerp(RootJointC0*CFrame.Angles(math.rad(-tiltX*10),0,math.rad(torsoTilt_Y)),deltaTime*10)
local legTilt_Y = math.clamp(-tiltY*20*(movementVector.Z ~= 0 and 1/(2^(1/2)) or 1.25),-60,60)
LeftHip.C0 = CFrame.Angles(0,math.rad(legTilt_Y),0)*LeftHipC0
RightHip.C0 = CFrame.Angles(0,math.rad(legTilt_Y),0)*RightHipC0
HumanoidRootPart.CFrame = CFrame.new(HRPPosition)*CFrame.Angles(HRProtX,CamerarotY,HRProtZ)
else
RootJoint.C0 = RootJointC0
LeftHip.C0 = LeftHipC0
RightHip.C0 = RightHipC0
end
Neck.C0 = NeckC0*CFrame.Angles(-CamerarotX-(HRProtX-TorsorotX),0,0)
local Tool = ToolEquipped()
local rightShoulder_C0 = RightShoulderC0
local leftShoulder_C0 = LeftShoulderC0
if Tool then
local armBobble = CFrame.Angles(math.rad(zRotation)*2,math.rad(bobbleX)*5,math.rad(bobbleY)*5)
local rightShoulder_Offset = CFrame.new((InFirstPerson() and 1.5 or 0), -1, 0)*armBobble
local leftShoulder_Offset = CFrame.new((InFirstPerson() and -1.5 or 0), -1, 0)*armBobble:Inverse()
local HRParmOffset = (RootJoint.C0*RootJointC0:Inverse()):Inverse()
rightShoulder_C0 = HRParmOffset*(RightShoulderC0+Vector3.yAxis)*CFrame.Angles(0,0,CamerarotX)*rightShoulder_Offset
leftShoulder_C0 = HRParmOffset*(LeftShoulderC0+Vector3.yAxis)*CFrame.Angles(0,0,-CamerarotX)*leftShoulder_Offset
end
RightShoulder.C0 = RightShoulder.C0:Lerp(rightShoulder_C0,deltaTime*10)
LeftShoulder.C0 = LeftShoulder.C0:Lerp(leftShoulder_C0,deltaTime*10)
if UserInputService:IsKeyDown(Enum.KeyCode.E) then
currentShoulder = 1
if holdStart == 0 then
holdStart = tick()
end
if tick()-holdStart > 0.25 then
leaning = true
end
elseif UserInputService:IsKeyDown(Enum.KeyCode.Q) then
currentShoulder = -1
if holdStart == 0 then
holdStart = tick()
end
if tick()-holdStart > 0.25 then
leaning = true
end
else
holdStart = 0
leaning = false
end
MakeInvisible(InFirstPerson())
old_finalRotation = finalRotation
old_cameraOffset = cameraOffset
old_shakeCFrame = shakeCFrame
oldX = cameraRotation.X
old_bobbleX = bobbleX
old_bobbleY = bobbleY
end)
UserInputService.InputChanged:Connect(function(Input,gameProcessedEvent)
if not gameProcessedEvent then
if Input.UserInputType == Enum.UserInputType.MouseMovement then
local Delta = Input.Delta
local X = cameraRotation.X - math.clamp(Delta.X/6,-10,10)
local Y = math.clamp(cameraRotation.Y - Delta.Y/4,-75,75)
cameraRotation = Vector2.new(X % 360,Y) * (UserInputService.MouseDeltaSensitivity)
elseif Input.UserInputType == Enum.UserInputType.MouseWheel then
cameraDistance = math.clamp(cameraDistance - Input.Position.Z,0,12)
end
end
end)