Make Ui cover the sun

Hello developers,

I’ve been trying to implement a system where I use an ImageHandleAdornment to replace the sun. After many hours of failed attemps i’ve come here as a last hope. “Moon” is refering to the ImageHandleAdornment which IS visible when I playtest the game however is not covering the sun.

For reference I’m trying to replicate this posts solution however with a ImageHandleAdornment:
strong text

local Lighting = game:GetService("Lighting")

local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui

local GameUI = PlayerGui:WaitForChild("GameUI")
local MoonUi = GameUI.MoonUI
local Moon = MoonUi.Moon

local distFromCam = 16

game:GetService("RunService").RenderStepped:Connect(function()
	local cameraPosition = workspace.CurrentCamera.CFrame.Position
	local sunDirection = Lighting:GetSunDirection()

	Moon.CFrame = CFrame.new(cameraPosition + sunDirection * distFromCam)
end)```
1 Like
local Lighting = game:GetService("Lighting")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera

local Object = Instance.new("Part")
Object.Parent = workspace.CurrentCamera
Object.Anchored = true;
Object.Size = Vector3.new(1,1,1);
Object.CanCollide = false;

local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui

local GameUI = PlayerGui:WaitForChild("GameUI")
local MoonUi = GameUI.MoonUI
local Moon = MoonUi.Moon

local DistanceFromCamera = 16
local MoonSize = 1

Moon.Adornee = Object

RunService.RenderStepped:Connect(function()
	local CameraPosition = workspace.CurrentCamera.CFrame.Position
	local SunDirection = Lighting:GetSunDirection()
	local MoonPosition = CameraPosition + SunDirection * DistanceFromCamera
	Moon.CFrame = CFrame.new(MoonPosition, CameraPosition)
	
	local Distance = (MoonPosition - CameraPosition).Magnitude
	local BaseSize = math.tan(math.rad(Camera.FieldOfView) / 2) * Distance * MoonSize
	Moon.Size = Vector2.new(BaseSize,BaseSize)
end)


image

However, based on the naming relating to the moon, you might want to use :GetMoonPosition() instead of :GetSunPosition()

3 Likes

Wow this worked perfectly tysm!

1 Like

I noticed it does do clipping into the map, so you might need to figure out where the map ends and adjust the size and distance from camera based on that.