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.
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?
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)
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.
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