Alwaysontop BillboardGui limited visiblity

So There is a billboardgui attached to this white neon part, I basically want the billboardgui to disable once the neon part is not visible to the player camera
The billboardgui must have Alwaysontop on to get a realistic lens flare effect like this:

In these 2 images below you can see the problem, in the first image you can see that even though the white neon part is not visible to the camera, the billboardgui is visible. The second image is what I should be getting, the white neon part is not visible = billboardgui is disabled


I have searched on the Devforum but haven’t found any solutions and I also tried scripting myself, but I failed since I dont know how to script

2 Likes

I believe that the AlwaysOnTop property has to be disabled on the BillboardGui.

You’d need to:

  • Raycast from the BillboardGui’s Adornee position to the camera position
  • Set BillboardGui.Enabled to true if nothing was hit, else false
  • Run this every Heartbeat

Insert a Script in the BillboardGui, set its RunContext to Client, then paste this in

local billboardGui = script.Parent
game:GetService("RunService").Heartbeat:Connect(function()
    -- do nothing if there's no adornee
    if billboardGui.Adornee == nil then 
        return
    end
    -- get the position to raycast from 
    local origin = if billboardGui.Adornee:IsA("Attachment") 
        then billboardGui.Adornee.WorldPosition
        else billboardGui.Adornee.Position
    -- get raycast direction
    local direction = workspace.CurrentCamera.CFrame.Position - origin
    local raycastResult = workspace:Raycast(
        origin,
        direction
    )
    -- set enabled depending on result of raycast
    billboardGui.Enabled = raycastResult == nil
end)

NOTES:

  • This only cares about the center of the Adornee (make sure you set this, btw). You’d need to do further checks for the rest of the part’s surface if you want, but for general purposes this should be alright as an effect
  • This script isn’t optimized in any way (I’d use tags to apply the effect and have magnitude/visibility checks before the raycast), but just to show you how it works. Don’t use too many of these all at the same time
1 Like

Thank you so much, this will help alot

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