Is it possible to increase the fov smoothly once touching a block?

What I want to do is when I touch a block, the Field Of View increases to 80, but smoothly for example,
like this, for i = 1,5 do camera.FieldOfView = (70+(i*2)).

2 Likes

You can use TweenService for this.

For this, you would use a service called "TweenService". Tweening any value will smoothly transition it to the desired outcome. Read code below:

--variables
local ts = game:GetService("TweenService")
local cam = workspace.CurrentCamera

local Time = 2 --time it takes to get to 80 FOV


--tween
ts:Create(cam, TweenInfo.new(Time), {FieldOfView = 80}):Play()

Adding on, there are different ways you can transition/move the camera to the desired FOV using EasingDirection and EasingStyle. For example, to smoothly add then take away speed, do this:

ts:Create(cam, TweenInfo.new(Time, Enum.EasingDirection.InOut, Enum.EasingStyle.Sine), {FieldOfView = 80}):Play()
2 Likes

Can you give me an example, I want to make it as if you are going fast once touching it for example, like a sprint fov script.

	key = string.lower(key)
	if string.byte(key) == 48 then
		running = true
		local keyConnection = mouse.KeyUp:connect(function (key)
			if string.byte(key) == 48 then
				running = false
			end
		end)
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
			wait()
		end
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 30
		repeat wait () until running == false
		keyConnection:disconnect()
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
			wait()
		end
	end
end)```

Well, I tried
for i = 1,5 do game.Workspace.CurrentCamera.FieldOfView = (80-(i*2)) wait() end
and it works perfectly fine for the sprint script, are u sure tween is more efficient?

You can read up on TweenServices documentation here: TweenService | Documentation - Roblox Creator Hub

You shouldn’t be creating a new Tween with:Create() every time you need to change the FOV, this also goes for TweenInfo. I would store all the tween instances you need in a table or module so you can reuse them.

For loops are ok, but for the smoothest effect, TweenService is the way to go. For loops go up in increments, which won’t always be smooth. However, TweenService is highly recommended for smooth transitions.

1 Like

Yes!

I didn’t use TweenService for this.
You can do this with two simple scripts.

ServerScript (Inside of the part you want to trigger it)

local HorrorFOV = game:GetService("ReplicatedStorage").HorrorFOV 
local Detector = script.Parent
local horrorfovactive = true

Detector.Touched:Connect(function(hit)
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if player and horrorfovactive then
		print("Player touched part, firing "..HorrorFOV.Name.."!")
		HorrorFOV:FireClient(player)
		print(HorrorFOV.Name.." has been fired!")
		horrorfovactive = false
		wait(3)
		-- horrorfovactive = true -- remove the "--" in front if want to scare multiple times in one spot
	end
end)

LocalScript (inside of StarterPlayer → StarterPlayerScripts )

game.ReplicatedStorage.HorrorFOV.OnClientEvent:Connect(function()
	local cam =	workspace.CurrentCamera
	if cam.FieldOfView == 70 then
		local stopFOVamt = 110
		while task.wait() do
			cam.FieldOfView += 1
			if cam.FieldOfView >= stopFOVamt then
				cam.FieldOfView = 110
				break
			end
		end
	wait(4)
		local stopFOVamt2 = 70
		while task.wait() do
			cam.FieldOfView -= 1
			if cam.FieldOfView <= stopFOVamt2 then
				cam.FieldOfView = 70
				break
			end
		end
	else
		cam.FieldOfView = 70
	end
end)


1 Like