Make value decrease as something gets further away?

I was just playing around and making a camera system that follows the player, but for some reason I just can’t figure out how to make the field of view camera increase as the player gets closer but decrease when they get further.

local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")

local localPlayer = Players.LocalPlayer
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")

local cameraView = Workspace:WaitForChild("CameraView")

local currentCamera = Workspace.CurrentCamera

task.wait(5)

currentCamera.CameraType = Enum.CameraType.Scriptable
currentCamera.CFrame = CFrame.lookAt(cameraView.Position, humanoidRootPart.Position)

local function followPlayer(deltaTimeSim)
	TweenService:Create(currentCamera, TweenInfo.new(0.1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {FieldOfView = (cameraView.Position + humanoidRootPart.Position).Magnitude / 5}):Play()
	TweenService:Create(currentCamera, TweenInfo.new(0.3, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {CFrame = CFrame.lookAt(cameraView.Position, humanoidRootPart.Position)}):Play()
end

RunService.PreSimulation:Connect(followPlayer)
{FieldOfView = 120 / (cameraView.Position - humanoidRootPart.Position).Magnitude * 5}

To invert it, I just made it use the max FoV size (120) and divide it by your calculations. Although I did have to switch /5 to *5. You can adjust 5 to whatever number makes it look the way that you want.

1 Like

Thanks, I felt stupid for not knowing this, works well

1 Like