Im making a fps game that uses a viewport frame with a gun and the arms, i made it so only when when the gun is equipped you get into first person. The script in question is the camera movement, as wobble effect and weapon sway, but it is costing too much on fps, what can i do to improve it?
The script:
local uis = game:GetService("UserInputService")
local runs = game:GetService("RunService")
local ts = game:GetService("TweenService")
local plr = game:GetService("Players").LocalPlayer
local plrChar = plr.Character
local viewport = script.Parent.Viewport
local charViewport = viewport:WaitForChild("WorldModel"):WaitForChild("Arms")
local gunPos = charViewport.GunPos
local viewportCamera = script.Parent.Viewport:WaitForChild("Camera")
local TurnX = 0
local TurnY = 0
local turn2 = 0
local function Lerp(a, b, t)
return a + (b - a) * t
end
local camti = TweenInfo.new(0.1)
--Not so important animations to when the gun is equipped
local previousVelocity = nil
local hrp = plrChar:WaitForChild("HumanoidRootPart")
local camera = workspace.CurrentCamera
local char = script.Parent
local hrp = plrChar:WaitForChild("HumanoidRootPart")
local rotFrequency, rotIntensity = 8, .5 -- camera rotation on z axis (frequency is how fast, intensity is how strong)
local verFrequency, verIntensity = 16, .125 -- camera movement on y axis (frequency is how fast, intensity is how strong)
local defWalkSpeed = 16 -- default walk speed (so that whenever the walk speed increases, the walk bob gets faster)
local function GetCurve(frequency, intensity)
return (math.sin(os.clock() * frequency) * intensity)
end
runs.Heartbeat:Connect(function(deltaTime)
if viewport.Visible == false then return end
--Viewport Camera
charViewport.GunPos.CFrame = charViewport["Right Arm"].RightGripAttachment.WorldCFrame
local MouseDelta = uis:GetMouseDelta()
if MouseDelta.Magnitude ~= 0 then
local factorX = 1
if MouseDelta.X > 0 then
factorX = MouseDelta.X / 10
elseif MouseDelta.X < 0 then
factorX = -MouseDelta.X / 10
end
local factorY = 1
if MouseDelta.Y > 0 then
factorY = MouseDelta.Y / 10
elseif MouseDelta.Y < 0 then
factorY = -MouseDelta.Y / 10
end
TurnY = Lerp(TurnY, math.clamp(MouseDelta.Y, -16, 16) * factorY, (10 * deltaTime))
TurnX = Lerp(TurnX, math.clamp(MouseDelta.X, -16, 16) * factorX, (10 * deltaTime))
else
TurnX = Lerp(TurnX, 0, (10 * deltaTime))
TurnY = Lerp(TurnY, 0, (10 * deltaTime))
end
local velocityY = hrp.AssemblyLinearVelocity.Y / defWalkSpeed / 10
ts:Create(viewportCamera, camti, {CFrame = CFrame.new(Vector3.new(-math.rad(TurnX) , 1.6 + velocityY - math.rad(TurnY), -math.rad(TurnX))) * CFrame.Angles(-math.rad(TurnY), -math.rad(TurnX), 0)}):Play()
--Player Camera
local velocity = math.round(Vector3.new(hrp.AssemblyLinearVelocity.X, 0, hrp.AssemblyLinearVelocity.Z).Magnitude)
if velocity == 0 then return end
defWalkSpeed = plrChar.Humanoid.WalkSpeed
turn2 = TurnX / 4
if previousVelocity and previousVelocity ~= velocity then
velocity = Lerp(previousVelocity, velocity, .25)
end
previousVelocity = velocity
camera.CFrame *= CFrame.new(0, GetCurve(verFrequency, verIntensity) * velocity / defWalkSpeed, 0) * CFrame.Angles(0, 0, (math.rad(GetCurve(rotFrequency, rotIntensity) * velocity / defWalkSpeed) + math.rad(turn2) ))
end)