Can I optimize this code more?

I’m making (or at least trying to make) a performant game that takes as few user resources as possible while trying not to look like a 2008 game. I wrote a custom camera script already and I want to know if it’s possible to optimize it

Here it is:

local Camera = workspace.CurrentCamera
local CameraPart = script:WaitForChild("CameraPart")
local Character = script.Parent.Parent.Parent
local Humanoid:Humanoid = Character.Humanoid
local HumanoidRootPart:Part = Character.HumanoidRootPart
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local UserGameSettings:UserGameSettings = UserSettings():GetService("UserGameSettings")
local RotX,RotY = 0,0
local LockCenterEnum = Enum.MouseBehavior.LockCenter
local Plus1Point5Vector = Vector3.new(0,1.5,0)
local Clamp = math.clamp
local Rad = math.rad
local CFrameNew = CFrame.new
local CFrameYXZ = CFrame.fromEulerAnglesYXZ

local RenderEvent = function(DT)
	local DeltaMove = UserInputService:GetMouseDelta()
	RotX,RotY = RotX-DeltaMove.Y*UserGameSettings.MouseSensitivity,RotY-DeltaMove.X*UserGameSettings.MouseSensitivity
	RotX = Clamp(RotX,-85,85)

	CameraPart.CFrame = CFrameNew(HumanoidRootPart.Position+Humanoid.CameraOffset+Plus1Point5Vector)*CFrameYXZ(Rad(RotX),Rad(RotY),0)
	Camera.CFrame = script.Camera.CFrame
end

Camera.CameraType = Enum.CameraType.Scriptable
UserInputService.MouseBehavior = LockCenterEnum

local Event = RunService.RenderStepped:Connect(RenderEvent)

UserInputService.WindowFocused:Connect(function()
	UserInputService.MouseBehavior = LockCenterEnum
end)

for i,v in pairs(Character:GetChildren()) do
	if v:IsA("BasePart") then
		v.LocalTransparencyModifier = 1
	end
	if v:IsA("Accessory") then
		v.Handle.LocalTransparencyModifier = 1
	end
end
2 Likes

Multiplying a preset vector is faster, do Vector3.yAxis*1.5

You’re defining RunService just to use it once, just get the service and connect it game:GetService("RunService").RenderStepped:Connect(RenderEvent)

how about not splitting the clamping to another line, and do it in 1 instead:
RotX,RotY = Clamp(RotX-DeltaMove.Y*UserGameSettings.MouseSensitivity,-85,85),RotY-DeltaMove.X*UserGameSettings.MouseSensitivity

maybe use elseif for the accessory, so that it doesnt run the IsA check twice if the instance was a part:

if v:IsA("BasePart") then
	v.LocalTransparencyModifier = 1
elseif v:IsA("Accessory") then
	v.Handle.LocalTransparencyModifier = 1
end

not much i can think of anymore, what is CameraPart and script.Camera for?

2 Likes

You gave me some good suggestions. But one of them I can’t apply to:

Since this is a custom camera. I also wanted to make it animatable. Since you can’t make Motor6d work with bare CFrames, I had to apply rotation first to parts. But on the whole, you told me actual good stuff. Thanks!

From what I see, the script does the job well enough already with minimal resource usage. Any more performance changes here would be negligible. It’s not worth sacrificing readability for optimization that can likely be done better elsewhere in the game.

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