I need help with my BillboardGUI light flare

Hi, DevForum. I’m working on a game and I have issues with my BillboardGUI light.

I need to know how I can make it so the image overlays on top of the black part, but as soon as the middle is blocked by a part, the whole image is disabled (just like in real life, you don’t see the flare unless the light source is completely blocked).

Blackhawk Rescue Mission 5 lights does exactly what I want, here’s an example:

Is there a script involved? Thank you.

1 Like

You could set the BillboardGUI AlwaysOnTop to true and check each frame if the light source is blocked by using a raycast. Then when the light is blocked, the image of the flare gets tweened out and back in when the light is revealed again. Code example:

local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")

local camera = workspace.CurrentCamera
local lightSource = -- The BasePart / Attachment the light is a child of
local billboardGUI = -- BillboardGUI that stores the flare image
local flareImage = -- ImageLabel for the flare

local rayParams = RaycastParams.new()

local flareOnTransparency = 0 -- Transparency of flare image when it's visible
local flareOffTransparency = 1 -- Transparency of flare image when it's invisible

RunService.RenderStepped:Connect(function()
	local lightPosition = -- Position for BaseParts / WorldPosition for Attachments
	local cameraPosition = camera.CFrame.Position
	local fromCameraToLight = lightPosition - cameraPosition -- Vector from camera to lightsource
	
	-- Cast a ray from camera to light source
	local rayResult = workspace:Raycast(cameraPosition, fromCameraToLight, rayParams)

	if rayResult then
		-- Turn flare off
		if flareImage.ImageTransparency > flareOnTransparency then return end -- Avoid tweening multiple times
		TweenService:Create(flareImage, TweenInfo.new(0.1), {ImageTransparency = flareOffTransparency}):Play()
	else
		-- Turn flare back on
		if flareImage.ImageTransparency < flareOffTransparency then return end -- Avoid tweening multiple times
		TweenService:Create(flareImage, TweenInfo.new(0.1), {ImageTransparency = flareOnTransparency}):Play()
	end
end)

Here’s the end result (Keep in mind a ray cast will hit a mesh’s collider which might not always match up with its visuals):

3 Likes