Hello, below is the code to what I hoped would be a simple dynamic depth of field effect for my game, using rays. The script is placed in StarterCharacterScripts.
local RunService = game:GetService("RunService")
local lighting = game:GetService("Lighting")
local depthOfField = game.Lighting.DepthOfField
local camera = workspace.CurrentCamera
function increaseIntesity()
for count = .3,1,.1 do
depthOfField.FarIntensity = count
wait()
end
end
function decreaseIntesity()
for count = 1,.3,-0.1 do
depthOfField.FarIntensity = count
wait()
end
end
function increaseFocusDistance()
for count = 30,100,2 do
depthOfField.FocusDistance = count
wait()
end
end
function decreaseFocusDistance()
for count = 100,30,-2 do
depthOfField.FocusDistance = count
wait()
end
end
local rayLength = 100
local viewportPoint = camera.ViewportSize / 2
local changeNumber = 40
RunService.RenderStepped:Connect(function()
local unitRay = camera:ViewportPointToRay(viewportPoint.X, viewportPoint.Y, 0)
local ray = Ray.new(unitRay.Origin, unitRay.Direction * rayLength)
local hit, position = workspace:FindPartOnRay(ray)
if (camera.CFrame.Position - position).magnitude < changeNumber then
print("first if statement running")
if depthOfField.FarIntensity == 0.3 and depthOfField.FocusDistance == 100 then -- not working
print("second if statement running")
increaseIntesity()
decreaseFocusDistance()
end
elseif (camera.CFrame.Position - position).magnitude > changeNumber or hit == nil then
if depthOfField.FarIntensity == 1 and depthOfField.FocusDistance == 30 then -- not working
increaseFocusDistance()
decreaseIntesity()
end
end
end)
The ray is working perfectly, however my issue is with the two if statements that I have labeled above, the DepthOfField values are set correctly in studio, and there are no error messages. This might be because i am using the if statement wrong or something to do with DepthOfField itself. I would appreciate some help, thanks.