Speed based FOV!

Original Creator @VitVerse

I just took his SpeedBasedFoV script and ran it into a heartbeat service, I know this is a duplicate, but I have seen no posts on speed based FOV or anything like it, this is also my first post. I hope this helps others looking for something like this.

Tutorial/Overview & Review

create a local script and put it into starter character scripts.

then copy the script, or follow the instructions below:

Always put local before defining, this won’t always be the case, but most times you have to have to make these variables local, you also don’t have to use the names I use, you can change them, but if you want to change a variables name, you’ll have to change it across the whole script.

First the script: Gets the player, its character, and then the characters child, the humanoid.

The script then: gets the players camera, walkspeed, which is called baseWalkSpeed due to it being called before the main function.

Finally, the script gets: the FOV or Field Of View of the camera that we got earlier, then it gets the Tween Service, defined as TS for short so we can have a smooth transition

We then use tweenInfo to define the information of the tween we will use later; you can change the easing style as you see fit, I just chose exponential because I liked how it slowed rapidly as it approached its target FOV.

Now we have everything to start our function, we first put; function SpeedtoFov (which is the name of our function) then a pair of parentheses, or you can press enter, and Roblox will put the parentheses in for you.

Still more defining: We get the movement-speed of the player overall by getting the player, then the players character, then the characters primary part, we get the velocity of the primary part, and finally the magnitude of said velocity, PHEW.

Now we create a ratio, a comparison of two values, we do this by dividing movementspeed by basewalkspeed in the variable by using a slash.

And then we create a variable for the new FOV, this variable adds the ratio and the basefov together, the reason we don’t multiply like in the original, is because, if the player is standing still, movement speed would = 0 and that would make the fov 0 as well.

now we make a new tween, we say TS:Create creating a new tween, we place parentheses, then inside these parentheses we put (camera(CurrentCamera), tweenInfo(Info of the tween we defined earlier), {FieldOfView = newFOV(Makes the tween affect the field of view and make it the new field of view)})

Finally, we say tween:Play() to play the tween and end to stop the function.

Alright, stay with me, last part!

We tell the game to get the service, “RunService” RunService gets HeartBeat and Connect, connects it to the function we made previously, just make sure to put the function in parentheses.

now make sure there are no red or blue errors and you’re

done!

here is the script:

-- Put this in StarterCharacter Scripts
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local camera = workspace.CurrentCamera

local baseWalkSpeed = humanoid.WalkSpeed
local baseFOV = camera.FieldOfView

local TS = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Exponential)

function SpeedtoFov()
	local WalkSpeed = humanoid.WalkSpeed
	local movementspeed = player.Character.PrimaryPart.Velocity.Magnitude
	local ratio = movementspeed / baseWalkSpeed
	local newFOV = baseFOV + ratio
	local tween = TS:Create(camera, tweenInfo, {FieldOfView = newFOV})
	tween:Play()
end

game:GetService("RunService").Heartbeat:Connect(SpeedtoFov)
6 Likes

thank you for the script! i was looking all over for one!

1 Like

No problem, I also might come back to this script to make edits, such as making it togglable safely, or allowing players to make fixed or changing numbers of different values (though you can already do that in the script)

1 Like

Found out if you die while your FOV is altered, it’ll stay that way, attempting to patch…

here are some suggestions for good practice.

  • do not abbreviate variables for services or anything. always describe everything by their full name.
    – your code will be hell to look at in a few months if you abbreviate variables. trust me

  • define all the services you use, and put them at the top of your script

2 Likes

I feel like using GetPropertyChangedSignal is better practice in this case because unless the walkspeed is changing literally every frame, the heartbeat is just running tweens and math every frame with no actual reason

also to fix the death thing, its because the humanoid is nil and it probably errors, you’d need to update the variable

humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(SpeedtoFov)

local function newCharacterAdded(newCharacter)
	humanoid = newCharacter:WaitForChild("Humanoid")
	humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(SpeedtoFov)
end

player.CharacterAdded:Connect(newCharacterAdded)
1 Like

Thanks a lot, I’ll be applying these to my script and updating the post.

1 Like

I’ll attempt to add this, im an amateur when it comes to understanding scripting though.

So the newCharacterAdded function is for the humanoid, and the Get property changed is for the change in values