How would I optimize/reformat this code to include other things such as viewmodels or camera bobbing?

Heya Everyone!!

I’ve found a camera swaying script from this DevForum post and I’m wondering on how would I optimize/reformat it to include other stuff such as viewmodels, camera bobbing, etc? (Basically, how would I add other camera/viewmodel-related code inside of it while feeling natural in a way?)

--[[SERVICES]]--
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

--[[VARIABLES]]--
local CurrentCamera = game.Workspace.CurrentCamera
local Turn = 0

--[[LERPING FUNCTION]]--
local function Lerp(A, B, T)
	return A + (B - A) * T
end

--[[MAIN/CAMERA SWAYING]]--
RunService:BindToRenderStep("CameraSway", Enum.RenderPriority.Camera.Value + 25, function(deltaTime)
	local MouseDelta = UserInputService:GetMouseDelta()
	Turn = Lerp(Turn, math.clamp(MouseDelta.X, -4, 3), (15 * deltaTime))
	CurrentCamera.CFrame = CurrentCamera.CFrame * CFrame.Angles(0, 0, math.rad(Turn))
end)

I mean you could create a variable to reference the viewmodel at the top.

And then you’d just add the viewmodel code (that’s it)

--[[SERVICES]]--
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

--[[VARIABLES]]--
local CurrentCamera = game.Workspace.CurrentCamera
local Viewmodel = path.to.viewmode
local Turn = 0

--[[LERPING FUNCTION]]--
local function Lerp(A, B, T)
	return A + (B - A) * T
end

--[[MAIN/CAMERA SWAYING]]--
RunService:BindToRenderStep("CameraSway", Enum.RenderPriority.Camera.Value + 25, function(deltaTime)
	local MouseDelta = UserInputService:GetMouseDelta()
	Turn = Lerp(Turn, math.clamp(MouseDelta.X, -4, 3), (15 * deltaTime))
	CurrentCamera.CFrame = CurrentCamera.CFrame * CFrame.Angles(0, 0, math.rad(Turn))
        Viewmodel.PrimaryPart:PivotTo(…)
end)

I don’t think there’s anything else to really change / add

1 Like

Yeah, I might’ve been overcomplicating myself. Thank you.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.