Hello, second resource of forever, I am releasing this fov controller system iβve made for uses cases where i needed to overlap or stack different fovs for sprinting, aiming, etc
--!strict
-- FOVController v0.0.3
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
-- Types
export type SourceConfig = {
Enabled: boolean?,
Value: number?,
CurrentValue: number?,
Exclusive: boolean?,
Priority: number?,
Instant: boolean?,
EnterSpeed: number?,
EnterEaseStyle: Enum.EasingStyle?,
EnterEaseDirection: Enum.EasingDirection?,
ExitSpeed: number?,
ExitEaseStyle: Enum.EasingStyle?,
ExitEaseDirection: Enum.EasingDirection?,
}
type Pulse = {
Amount: number,
Duration: number,
Age: number,
}
type Source = {
Enabled: boolean,
Value: number,
CurrentValue: number,
Exclusive: boolean,
Priority: number,
Instant: boolean,
EnterSpeed: number?,
EnterEaseStyle: Enum.EasingStyle?,
EnterEaseDirection: Enum.EasingDirection?,
ExitSpeed: number?,
ExitEaseStyle: Enum.EasingStyle?,
ExitEaseDirection: Enum.EasingDirection?,
Pulses: { Pulse },
PulseCurrent: number,
_PulseId: number,
_SnapEnter: boolean,
_ResetTask: thread?,
}
type FOVControllerImpl = {
__index: FOVControllerImpl,
BaseFOV: number,
TargetFOV: number,
CurrentFOV: number,
DefaultEaseStyle: Enum.EasingStyle,
DefaultEaseDirection: Enum.EasingDirection,
DefaultSpeed: number,
Sources: { [string]: Source },
_Connection: RBXScriptConnection?,
RegisterSource: (self: FOVControllerImpl, name: string, config: SourceConfig?) -> Source,
RemoveSource: (self: FOVControllerImpl, name: string) -> (),
SetSourceEnabled: (self: FOVControllerImpl, name: string, enabled: boolean) -> (),
SetSourceValue: (self: FOVControllerImpl, name: string, value: number) -> (),
SetSourcePriority: (self: FOVControllerImpl, name: string, priority: number) -> (),
SetSourceMode: (self: FOVControllerImpl, name: string, exclusive: boolean) -> (),
SetSource: (self: FOVControllerImpl, name: string, enabled: boolean?, value: number?, priority: number?) -> (),
PushPulse: (self: FOVControllerImpl, name: string, amount: number, duration: number) -> (),
PushSnapEnter: (self: FOVControllerImpl, name: string, value: number, holdDuration: number?) -> (),
PushInstant: (self: FOVControllerImpl, name: string, value: number, holdDuration: number?) -> (),
_UpdatePulseStack: (self: FOVControllerImpl, source: Source, dt: number) -> number,
UpdateSources: (self: FOVControllerImpl, dt: number) -> (),
ComputeTarget: (self: FOVControllerImpl) -> number,
Start: (self: FOVControllerImpl, camera: Camera) -> (),
Stop: (self: FOVControllerImpl) -> (),
}
-- Module
local FOVController = {} :: FOVControllerImpl
FOVController.__index = FOVController
FOVController.BaseFOV = 70
FOVController.TargetFOV = 70
FOVController.CurrentFOV = 70
FOVController.DefaultEaseStyle = Enum.EasingStyle.Sine
FOVController.DefaultEaseDirection = Enum.EasingDirection.Out
FOVController.DefaultSpeed = 12
FOVController.Sources = {}
FOVController._Connection = nil
local EPSILON = 0.001
local DEFAULT_SOURCE: Source = {
Enabled = false,
Value = 0,
CurrentValue = 0,
Exclusive = true,
Priority = 1,
Instant = false,
EnterSpeed = nil,
EnterEaseStyle = nil,
EnterEaseDirection = nil,
ExitSpeed = nil,
ExitEaseStyle = nil,
ExitEaseDirection = nil,
Pulses = {},
PulseCurrent = 0,
_PulseId = 0,
_SnapEnter = false,
_ResetTask = nil,
}
local function cloneSource(source: Source): Source
return {
Enabled = source.Enabled,
Value = source.Value,
CurrentValue = source.CurrentValue,
Exclusive = source.Exclusive,
Priority = source.Priority,
Instant = source.Instant,
EnterSpeed = source.EnterSpeed,
EnterEaseStyle = source.EnterEaseStyle,
EnterEaseDirection = source.EnterEaseDirection,
ExitSpeed = source.ExitSpeed,
ExitEaseStyle = source.ExitEaseStyle,
ExitEaseDirection = source.ExitEaseDirection,
Pulses = {},
PulseCurrent = source.PulseCurrent,
_PulseId = source._PulseId,
_SnapEnter = source._SnapEnter,
_ResetTask = nil,
}
end
local function GetEasedStep(
current: number,
target: number,
dt: number,
speed: number,
easeStyle: Enum.EasingStyle,
easeDirection: Enum.EasingDirection
): number
local alpha = math.clamp(dt * speed, 0, 1)
local easedAlpha = TweenService:GetValue(alpha, easeStyle, easeDirection)
return current + (target - current) * easedAlpha
end
function FOVController:RegisterSource(name: string, config: SourceConfig?): Source
if self.Sources[name] then
return self.Sources[name]
end
local source = cloneSource(DEFAULT_SOURCE)
if config then
if config.Enabled ~= nil then source.Enabled = config.Enabled :: boolean end
if config.Value ~= nil then source.Value = config.Value :: number end
if config.CurrentValue ~= nil then source.CurrentValue = config.CurrentValue :: number end
if config.Exclusive ~= nil then source.Exclusive = config.Exclusive :: boolean end
if config.Priority ~= nil then source.Priority = config.Priority :: number end
if config.Instant ~= nil then source.Instant = config.Instant :: boolean end
if config.EnterSpeed ~= nil then source.EnterSpeed = config.EnterSpeed end
if config.EnterEaseStyle ~= nil then source.EnterEaseStyle = config.EnterEaseStyle end
if config.EnterEaseDirection ~= nil then source.EnterEaseDirection = config.EnterEaseDirection end
if config.ExitSpeed ~= nil then source.ExitSpeed = config.ExitSpeed end
if config.ExitEaseStyle ~= nil then source.ExitEaseStyle = config.ExitEaseStyle end
if config.ExitEaseDirection ~= nil then source.ExitEaseDirection = config.ExitEaseDirection end
end
self.Sources[name] = source
return source
end
function FOVController:RemoveSource(name: string): ()
self.Sources[name] = nil
end
function FOVController:SetSourceEnabled(name: string, enabled: boolean): ()
local source = self.Sources[name]
if not source then return end
source.Enabled = enabled
end
function FOVController:SetSourceValue(name: string, value: number): ()
local source = self.Sources[name]
if not source then return end
source.Value = value
end
function FOVController:SetSourcePriority(name: string, priority: number): ()
local source = self.Sources[name]
if not source then return end
source.Priority = priority
end
function FOVController:SetSourceMode(name: string, exclusive: boolean): ()
local source = self.Sources[name]
if not source then return end
source.Exclusive = exclusive
end
function FOVController:SetSource(name: string, enabled: boolean?, value: number?, priority: number?): ()
local source = self.Sources[name]
if not source then return end
if enabled ~= nil then
source.Enabled = enabled :: boolean
end
if value ~= nil then
source.Value = value :: number
end
if priority ~= nil then
source.Priority = priority :: number
end
end
-- Holds Amount at full value for duration seconds, then removes itself.
-- The source's ExitSpeed easing carries CurrentValue back to 0 after that.
function FOVController:PushPulse(name: string, amount: number, duration: number): ()
local source = self.Sources[name]
if not source then return end
source.Enabled = true
local pulse: Pulse = {
Amount = amount,
Duration = math.max(duration, 0.001),
Age = 0,
}
table.insert(source.Pulses, pulse)
end
-- Snaps CurrentValue instantly on enter, then exits with easing.
-- holdDuration: seconds before Value resets to 0 and exit easing kicks in.
-- holdDuration = nil: never auto-resets, you call SetSourceValue manually.
function FOVController:PushSnapEnter(name: string, value: number, holdDuration: number?): ()
local source = self.Sources[name]
if not source then return end
source.Enabled = true
source.Value = value
source.CurrentValue = value
source._SnapEnter = true
if source._ResetTask then
task.cancel(source._ResetTask)
source._ResetTask = nil
end
if holdDuration ~= nil then
source._ResetTask = task.delay(holdDuration :: number, function()
source.Value = 0
source._ResetTask = nil
end)
end
end
-- Snaps both enter and exit instantly (no easing at all).
-- holdDuration: seconds before Value resets to 0.
-- holdDuration = nil: never auto-resets.
function FOVController:PushInstant(name: string, value: number, holdDuration: number?): ()
local source = self.Sources[name]
if not source then return end
source.Enabled = true
source.Value = value
source.CurrentValue = value
if source._ResetTask then
task.cancel(source._ResetTask)
source._ResetTask = nil
end
if holdDuration ~= nil then
source._ResetTask = task.delay(holdDuration :: number, function()
source.Value = 0
source._ResetTask = nil
end)
end
end
function FOVController:_UpdatePulseStack(source: Source, dt: number): number
local total = 0
local pulses = source.Pulses
for i = #pulses, 1, -1 do
local pulse = pulses[i]
pulse.Age += dt
if pulse.Age >= pulse.Duration then
table.remove(pulses, i)
else
total += pulse.Amount
end
end
return total
end
function FOVController:UpdateSources(dt: number): ()
local highestExclusivePriority = -math.huge
for _, source in pairs(self.Sources) do
if source.Enabled and source.Exclusive and source.Priority > highestExclusivePriority then
highestExclusivePriority = source.Priority
end
end
for _, source in pairs(self.Sources) do
local pulseValue = self:_UpdatePulseStack(source, dt)
local targetValue = 0
if source.Enabled then
if source.Exclusive then
if source.Priority >= highestExclusivePriority then
targetValue = source.Value + pulseValue
end
else
targetValue = source.Value + pulseValue
end
end
if source.Instant then
source.CurrentValue = targetValue
elseif source._SnapEnter then
source._SnapEnter = false
else
local movingTowardValue = targetValue ~= 0
local speed = if movingTowardValue then source.EnterSpeed else source.ExitSpeed
local easeStyle = if movingTowardValue then source.EnterEaseStyle else source.ExitEaseStyle
local easeDirection = if movingTowardValue then source.EnterEaseDirection else source.ExitEaseDirection
source.CurrentValue = GetEasedStep(
source.CurrentValue,
targetValue,
dt,
speed or self.DefaultSpeed,
easeStyle or self.DefaultEaseStyle,
easeDirection or self.DefaultEaseDirection
)
if math.abs(source.CurrentValue) <= EPSILON and targetValue == 0 then
source.CurrentValue = 0
end
end
end
end
function FOVController:ComputeTarget(): number
local fov = self.BaseFOV
for _, source in pairs(self.Sources) do
fov += source.CurrentValue
end
return fov
end
function FOVController:Start(camera: Camera): ()
if self._Connection then
self._Connection:Disconnect()
self._Connection = nil
end
self._Connection = RunService.RenderStepped:Connect(function(dt: number)
if not camera or not camera.Parent then return end
self:UpdateSources(dt)
self.TargetFOV = self:ComputeTarget()
self.CurrentFOV = self.TargetFOV
camera.FieldOfView = self.TargetFOV
end)
end
function FOVController:Stop(): ()
if self._Connection then
self._Connection:Disconnect()
self._Connection = nil
end
end
-- Default gameplay sources
FOVController:RegisterSource("Sprint", {
Enabled = false,
Value = 15,
Priority = 1,
Exclusive = true,
Instant = false,
EnterSpeed = 3,
EnterEaseStyle = Enum.EasingStyle.Sine,
EnterEaseDirection = Enum.EasingDirection.Out,
ExitSpeed = 4,
ExitEaseStyle = Enum.EasingStyle.Sine,
ExitEaseDirection = Enum.EasingDirection.Out,
})
FOVController:RegisterSource("Aim", {
Enabled = false,
Value = -12,
Priority = 2,
Exclusive = true,
Instant = false,
EnterSpeed = 4,
EnterEaseStyle = Enum.EasingStyle.Sine,
EnterEaseDirection = Enum.EasingDirection.Out,
ExitSpeed = 4,
ExitEaseStyle = Enum.EasingStyle.Sine,
ExitEaseDirection = Enum.EasingDirection.Out,
})
FOVController:RegisterSource("Recoil", {
Enabled = false,
Value = 1,
Priority = 2,
Exclusive = true,
Instant = false,
EnterSpeed = 22,
EnterEaseStyle = Enum.EasingStyle.Back,
EnterEaseDirection = Enum.EasingDirection.Out,
ExitSpeed = 16,
ExitEaseStyle = Enum.EasingStyle.Quad,
ExitEaseDirection = Enum.EasingDirection.Out,
})
FOVController:RegisterSource("Visualizer", {
Enabled = true,
Value = 0,
Exclusive = false,
Priority = 0,
Instant = false,
EnterSpeed = 8,
ExitSpeed = 3,
EnterEaseStyle = Enum.EasingStyle.Sine,
EnterEaseDirection = Enum.EasingDirection.Out,
ExitEaseStyle = Enum.EasingStyle.Quad,
ExitEaseDirection = Enum.EasingDirection.Out,
})
return FOVController
Setup :
task.wait()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Modules = ReplicatedStorage:WaitForChild("Modules")
local FovController = require(Modules.FOVController)
local camera = workspace.CurrentCamera
local start_fov = FovController:Start(camera)
Methods :
FOVController:RemoveSource(name: string) β ()
Unregisters and removes a source entirely. Its contribution is immediately
gone from ComputeTarget on the next frame.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
FOVController:SetSourceEnabled(name: string, enabled: boolean) β ()
Enables or disables a source. When disabled, the sourceβs target becomes 0
and it eases back via its ExitSpeed.
Example:
FOVController:SetSourceEnabled(βSprintβ, true) β player starts sprinting
FOVController:SetSourceEnabled(βSprintβ, false) β player stops sprinting
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
FOVController:SetSourceValue(name: string, value: number) β ()
Updates the FOV offset a source contributes while it is enabled.
Does not enable or disable the source.
Example:
β Scale sprint FOV by movement speed
FOVController:SetSourceValue(βSprintβ, speed * 0.3)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
FOVController:SetSourcePriority(name: string, priority: number) β ()
Changes the priority of an exclusive source at runtime.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
FOVController:SetSourceMode(name: string, exclusive: boolean) β ()
Switches a source between exclusive and additive mode at runtime.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
FOVController:SetSource(name, enabled?, value?, priority?) β ()
Convenience method to set multiple source properties in one call.
Any nil argument is ignored β only provided fields are updated.
Example:
FOVController:SetSource(βAimβ, true, -15, 2)
FOVController:SetSource(βSprintβ, false, nil, nil) β only disable
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
FOVController:PushPulse(name: string, amount: number, duration: number) β ()
Adds a transient pulse to a sourceβs pulse stack. The pulse holds its full
Amount for duration seconds, then removes itself. After removal, the
sourceβs ExitSpeed easing carries CurrentValue back to 0.
Multiple pulses stack additively. A new pulse does not cancel existing ones.
duration β how long to hold the full offset before releasing
amount β FOV offset added by this pulse (stacks with source.Value)
Example:
β On gunshot: hold +8 FOV for 0.1s, then ease back at ExitSpeed
FOVController:PushPulse(βRecoilβ, 8, 0.1)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
FOVController:PushSnapEnter(name, value, holdDuration?) β ()
Sets CurrentValue to value instantly on the current frame (no enter easing),
then after holdDuration seconds resets Value to 0 and lets the sourceβs
ExitSpeed easing carry CurrentValue back down.
If holdDuration is nil, Value is never auto-reset β you must call
SetSourceValue(name, 0) manually when you want the exit to begin.
Calling PushSnapEnter again before holdDuration expires cancels the
previous reset task and starts a new hold from the new value.
Best for: beat-synced effects, hit reactions, anything that must be
frame-perfect on the way up but can ease gracefully on the way down.
Example:
β Snap up on beat, ease back down after 0.08s
FOVController:PushSnapEnter(βVisualizerβ, 18, 0.08)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
FOVController:PushInstant(name, value, holdDuration?) β ()
Snaps CurrentValue to value immediately. If the source has Instant = true,
it will also snap back to 0 instantly when holdDuration expires.
If Instant = false on the source, exit will still use ExitSpeed easing.
If holdDuration is nil, Value is never auto-reset.
Calling PushInstant again before holdDuration expires cancels the previous
reset task and restarts the hold.
Best for: effects where both the pop and the recovery must be instantaneous,
such as a hard cut flash effect.
Example:
β Hard snap up, hard snap back after 0.05s
FOVController:PushInstant(βVisualizerβ, 25, 0.05)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
FOVController:Start(camera: Camera) β ()
Connects FOVController to RenderStepped and begins driving camera.FieldOfView
every frame. Disconnects any existing connection first, so it is safe to
call multiple times (e.g. after a camera change).
Example:
FOVController:Start(workspace.CurrentCamera)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
FOVController:Stop() β ()
Disconnects the RenderStepped connection. The cameraβs FieldOfView stops
being updated. Source state is preserved β calling Start again resumes
from where it left off.
Structure :
All of these values can be modified to your liking, for the value to be positive, just put the number, if you want it to be negative, do β-numberβ
Value = 15,
Priority = 1,
EnterSpeed = 3,
EnterEaseStyle = Enum.EasingStyle.Sine,
EnterEaseDirection = Enum.EasingDirection.Out,
ExitSpeed = 5,
ExitEaseStyle = Enum.EasingStyle.Sine,
ExitEaseDirection = Enum.EasingDirection.Out,