FOV changed based off character velocity

How to make your FOV change when you move

by hyunjinvinyl

Author Avatar

INTRO

If you're someone like me, you'll probably enjoy making games with camera movement. Games like these feel much more lively, and I'm sure you enjoy playing them alot more too. After reading this, you should have learned how to change a players FOV based off their movement.

SETTING UP Before actually writing the main script, you will need to set up your variables. Open a LocalScript in StarterPlayer - StarterCharacterScripts. It doesn't have to be named anything special, but to keep it organised we'll be naming it MovementFOV. Once you've opened your script, delete the print line and we can start setting up the variables!

local LocalPlayer = game.Players.LocalPlayer
local camera = game.Workspace.CurrentCamera
local tweenService = game:GetService("TweenService")

These statements set up what is needed in the script and clarifies what is going to be referred to. The word after local is what you're naming the variable, and after the = symbol is what it describes. As you can see, we go through to what we specifically want to refer to as though we're going through folders. For example, on the first line we are getting the LocalPlayer from Players which is in the game.

local minFOV = 70
local maxFOV = 120

This here tells us the minimum FOV we want and the maximum FOV. This time, when setting up the variable, we instead gave a number value instead of an object. These will be referred to later in the script. Feel free to change these numbers how you like.

local mostVelocity = Vector3.new()

In this variable we are creating a new vector, which can be altered and manipulated later in the script.

CODING Here we will be writing the actual script. As we have finally got what we need to get started. As you can see throughout this code, we are referencing different variables we set up earlier. If you changed the names of some variables earlier, don't forget to change them here too!

game:GetService("RunService").Heartbeat:Connect(function()
	local hum = LocalPlayer.Character:WaitForChild("Humanoid")
	local velocity = LocalPlayer.Character:WaitForChild("HumanoidRootPart").Velocity
	if hum:GetState() ~= Enum.HumanoidStateType.Freefall or velocity.Magnitude > mostVelocity.Magnitude then
		mostVelocity = velocity
	end
	local targetFOV = minFOV + (maxFOV - minFOV) * (mostVelocity.magnitude / 50)
	local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
	local tween = tweenService:Create(camera, tweenInfo, {FieldOfView = targetFOV}):Play()
end)

As you can see, we set up more variables here. These are not grouped with the earlier variables, as unlike the earlier ones they are not being used as shortcuts. The first part of this detects the player movement. The second half is what actually changes the FOV.

END Afterwards, this is what the script should look like put together.

local LocalPlayer = game.Players.LocalPlayer
local camera = game.Workspace.CurrentCamera
local tweenService = game:GetService("TweenService")

local minFOV = 70
local maxFOV = 120

local mostVelocity = Vector3.new()

game:GetService("RunService").Heartbeat:Connect(function()
	local hum = LocalPlayer.Character:WaitForChild("Humanoid")
	local velocity = LocalPlayer.Character:WaitForChild("HumanoidRootPart").Velocity
	if hum:GetState() ~= Enum.HumanoidStateType.Freefall or velocity.Magnitude > mostVelocity.Magnitude then
		mostVelocity = velocity
	end
	local targetFOV = minFOV + (maxFOV - minFOV) * (mostVelocity.magnitude / 50)
	local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
	local tween = tweenService:Create(camera, tweenInfo, {FieldOfView = targetFOV}):Play()
end)

And this is where it should be.

image|296x92

END

I hope this helped you guys! I'm not the greatest at writing tutorials, but I tried. Please give me feedback on my wording and formatting, thanks for reading! - chai

View in-game to comment, award, and more!