How do you increase Blur the farther you are away from something?

Greetings devs,

I’m trying to add a feature where the farther you are away from a specific part, the more blur there is. This is what i’m trying to achieve.


The code I currently have doesn’t include not having a Blur within a zone, nor is it functional in the first place.

local camera = game.Workspace.CurrentCamera
local part = game.Workspace.PlayerSpawn

local depthOfField = Instance.new("DepthOfFieldEffect")
depthOfField.Enabled = true
depthOfField.Parent = camera

local maxBlur = 10
local maxDistance = 50

while true do
	local distance = (part.Position - camera.CFrame.Position).Magnitude
	local blurIntensity = math.min(distance / maxDistance, 1) * maxBlur

	depthOfField.FocusDistance = distance
	depthOfField.InFocusRadius = blurIntensity

	wait(0.1)
end

I’ve used the AI Assistant, however I knew that wouldn’t work when it told me to put a LocalScript in ServerScriptService

2 Likes

The DepthOfFieldEffect simulates a camera lens by blurring parts of a scene not in focus. Distant objects can be blurred or this effect can be used to focus on specific parts of a scene, like an item in an in-game shop.

Try playing around with it.

By the way, Localscripts don’t work in ServerScriptService.

1 Like

That code from AI Assistant definitely doesn’t work.
Create a LocalScript and place it in StarterPlayerScripts.
Insert this code:

local part = game.Workspace:WaitForChild("SpawnLocation")
local player = game.Players.LocalPlayer
local camera = player

local blurEffect = Instance.new("BlurEffect")
blurEffect.Parent = game.Lighting
blurEffect.Enabled = true
blurEffect.Size = 0

while task.wait(0.1) do
	local distance = (part.Position - player.Character.HumanoidRootPart.Position).Magnitude
	
	blurEffect.Size = distance
end

Hope this helps!

1 Like

It does indeed work, thanks so much!

Edit: I changed it to blurEffect.Size = distance / 16 because it was too strong/sensitive.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.