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)