Clamping Transparency based on Distance?

I’m trying to make a Parts transparency be determined on a Players distance; this is so if the Player is outside a certain radius then it’ll no longer be visible.

local Distance = (Camera.CFrame.Position - Part.Position).Magnitude
Part.Transparency = math.clamp(Distance * 0.01, 0, 1)

Above is the code I’m currently working with and it works fine for most parts.
However, I only want the Transparency to take effect if the Camera is outside of a magnitude of 200 and when within it’d be back at 0?

Does anyone have any suggestions?

I’ve tried different maths to get a result

(Distance×2)×0.0025

However still not what I’m looking for.

Add a condition that checks, if the camera is outside of a magnitude of 200 before making the part transparent.

If done that too but because of the distance at the time it either goes straight to 1 or straight to 0 with no fading.

However I’ve got a new equation to try when I’m home:

local Distance = (Camera.CFrame.Position - Part.Position).Magnitude

local Range = 200
local Eq = (Distance - Range) / 100

local Transparency = math.clamp(Eq, 0, 1)

Hello,

I’d probably try and do something like this:

local camera = workspace.CurrentCamera
local part = workspace.Part

local visibilityRadius  = 200

function updateTransparency(part)
    local distance = (camera.CFrame.Position - part.Position).Magnitude
    
    if distance <= visibilityRadius then
        part.Transparency = 0
    else
        part.Transparency = math.clamp((distance - visibilityRadius) * 0.01, 0, 1)
    end
end


game:GetService("RunService").RenderStepped:Connect(function()
    updateTransparency(part)
end)

Connecting it to each frame is of course up to you, but I would do that to keep the effect constant.
If you have any questions or further issues let me know.

1 Like

this should do

local min = 200 -- distance beyond which it starts fading
local max = 500 -- distance beyond which its fully transparent
part.Transparency = (dist - min) / (max - min)

theres no need for clamping it because transparency only works between 0 and 1, even though it can be set beyond those values it doesnt make any difference

1 Like