Help in making this script more efficient

Hello! I have this cool camera effect where it follows the mouse, then hangs back a bit when the mouse stops moving. But, I want to make it more efficient and less memory-consuming.

SCIRPT:

local maxTilt = 12
local tweenInfo = TweenInfo.new(.1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local camera = workspace.CurrentCamera
local DefaultCFrame = game.Workspace.MainCamera

local connection = game:GetService("RunService").RenderStepped:Connect(function()
	ts:Create(camera,tweenInfo,{CFrame = DefaultCFrame.CFrame * CFrame.Angles(math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),0)}):Play()
end)

you should use lerping method if you want to smoothly update the camera on RenderStepped

multiplying the percentages by 2 because previously it was possible to only get 50% of the maxTilt

also if you are not going to disconnect that connection you dont really need a variable for it

local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
local camera = Workspace.CurrentCamera
local DefaultCFrame = Workspace.MainCamera

local maxTilt = 12
RunService.RenderStepped:Connect(function(dt)
	local X = math.rad(2*(mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX  * -maxTilt)
	-- which could be simplified to (2*mouse.X/mouse.ViewSizeX - 1) * -maxTilt
	local Y = math.rad(2*(mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY  * -maxTilt)
	-- the same for Y
	camera.CFrame = camera.CFrame:Lerp(DefaultCFrame.CFrame * CFrame.Angles(Y, X, 0), dt)
end)
1 Like

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