Problem with planet lighting script

(I don’t know how to explain well, I hope you understand what im saying through these amalgamation of words.)

I have a lighting script that works by positioning the sun behind the closest part with “Fire” inside it to create artificial light for planets, it works fine but it has a flaw, It only affects the X position of the sun (probably GeographicLatitude) and not the Y position of it. I’ve been trying so much different solutions but none of them are suitable or are just broken.

Please take to note i am a beginner programmer and not that skilled.

Here is the code to the script:

Lighting = game:GetService('Lighting')
Lighting.ClockTime = 6

local p = Instance.new('Part')

lights = {}
for _,obj in pairs(workspace:GetDescendants()) do
	if obj.Name == 'Fire' then
		table.insert(lights,obj.Parent)
		obj.Parent.CastShadow = false
	end
end

game:GetService('RunService').RenderStepped:Connect(function()
	local cam = workspace.CurrentCamera
	local closest = nil
	for _,p in pairs(lights) do
		local d = (p.Position - cam.CFrame.Position).Magnitude
		if closest == nil or d < closest[2] then
			closest = {p,d}
		end
	end
	if closest then
		closest = closest[1]
		p.CFrame = CFrame.new(closest.Position,cam.CFrame.Position)
		Lighting.GeographicLatitude = math.deg(2)-p.Orientation.Y
		Lighting.Brightness = 500 / math.sqrt((closest.Position - cam.CFrame.Position).Magnitude)
	end
end)

Help would be appreciated ^^

2 Likes

Hi @PigeonMap

You need to modify ClockTime or TimeOfDay for Y axis but i don’t know how to script that personally.

It does work but moves the sun to the left or right if the GeographicLatitude is above 0.

1 Like

You can try but keep in mind that I haven’t tried and i cannot gurantee it will work:

local Lighting = game:GetService("Lighting")
Lighting.ClockTime = 6

local p = Instance.new("Part")
local lights = {}

for _, obj in pairs(workspace:GetDescendants()) do
    if obj.Name == "Fire" then
        table.insert(lights, obj.Parent)
        obj.Parent.CastShadow = false
    end
end

game:GetService("RunService").RenderStepped:Connect(function()
    local cam = workspace.CurrentCamera
    local closest = nil

    for _, lightPart in pairs(lights) do
        local d = (lightPart.Position - cam.CFrame.Position).Magnitude
        if closest == nil or d < closest[2] then
            closest = {lightPart, d}
        end
    end

    if closest then
        closest = closest[1]
        p.CFrame = CFrame.new(closest.Position, cam.CFrame.Position)
        
        -- Adjust sun position on both axes
        local direction = (closest.Position - cam.CFrame.Position).unit
        Lighting.GeographicLatitude = math.deg(math.asin(direction.Y)) -- Adjust vertical (Y)
        Lighting.ClockTime = 12 + (direction.X * 6) -- Adjust horizontal (X)

        -- Adjust brightness based on distance
        Lighting.Brightness = 500 / math.sqrt((closest.Position - cam.CFrame.Position).Magnitude)
    end
end)

It works, but the sun doesn’t position itself behind the star.

1 Like

I don’t really know how to make that, You can look through old devforum post that is similar to yours request :

I’ve looked through that forum post before, i unfortunately didn’t get the results i liked.

1 Like

have you tried using:

local Lighting = game:GetService("Lighting")

local atan2 = math.atan2
local sqrt = math.sqrt
local tau = math.pi * 2

dir = (closest.Position - cam.CFrame.Position).unit

local lf = 1
local lat = atan2(dir.Z, sqrt(dir.X^2 + dir.Y^2))
local lon = atan2(dir.Y * lf, dir.X * lf)

local geoLatitude = (lat / tau) * 360 + 23.5
local clockTime = (lon / tau) * 24 - 6

Lighting.GeographicLatitude = geoLatitude
Lighting.ClockTime = clockTime % 24

I haven’t used that yet, I’m confused on where to put it in the code, Could you please point out where? (im stupid)

Full Script:

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

local cam = workspace.CurrentCamera

Lighting.ClockTime = 6

local atan2 = math.atan2
local sqrt = math.sqrt
local tau = math.pi * 2

local p = Instance.new("Part")
local lights = {}

for _, obj in pairs(workspace:GetDescendants()) do
	if obj.Name == "Fire" then
		table.insert(lights, obj.Parent)
		obj.Parent.CastShadow = false
	end
end

RunService.RenderStepped:Connect(function()
	local closest = nil

	for _, lightPart in pairs(lights) do
		local d = (lightPart.Position - cam.CFrame.Position).Magnitude
		if closest == nil or d < closest[2] then
			closest = {lightPart, d}
		end
	end

	if closest then
		closest = closest[1]
		p.CFrame = CFrame.new(closest.Position, cam.CFrame.Position)

		local dir = (closest.Position - cam.CFrame.Position).unit

		local lf = 1
		local lat = atan2(dir.Z, sqrt(dir.X^2 + dir.Y^2))
		local lon = atan2(dir.Y * lf, dir.X * lf)

		local geoLatitude = (lat / tau) * 360 + 23.5
		local clockTime = (lon / tau) * 24 - 6

		Lighting.GeographicLatitude = geoLatitude
		Lighting.ClockTime = clockTime % 24

		-- Adjust brightness based on distance
		Lighting.Brightness = 500 / math.sqrt((closest.Position - cam.CFrame.Position).Magnitude)
	end
end)

It works great! but instead of the sun, its the moon. I dont know much about these stuff, It would be appreciated if you point out what value i should change! ^^

(Very sorry for the late reply though.)

1 Like

Nevermind, I got it fixed!

Thank you for helping me!
Heres the code:

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

local cam = workspace.CurrentCamera

Lighting.ClockTime = 6

local atan2 = math.atan2
local sqrt = math.sqrt
local tau = math.pi * 2

local p = Instance.new("Part")
local lights = {}

for _, obj in pairs(workspace:GetDescendants()) do
	if obj.Name == "Fire" then
		table.insert(lights, obj.Parent)
		obj.Parent.CastShadow = false
	end
end

RunService.RenderStepped:Connect(function()
	local closest = nil

	for _, lightPart in pairs(lights) do
		local d = (lightPart.Position - cam.CFrame.Position).Magnitude
		if closest == nil or d < closest[2] then
			closest = {lightPart, d}
		end
	end

	if closest then
		closest = closest[1]
		p.CFrame = CFrame.new(closest.Position, cam.CFrame.Position)

		local dir = (closest.Position - cam.CFrame.Position).unit

		local lf = -1
		local lat = atan2(dir.Z, sqrt(dir.X^2 + dir.Y^2))
		local lon = atan2(dir.Y * lf, dir.X * lf)

		local geoLatitude = (lat / tau) * 360 + 23.5
		local clockTime = (lon / tau) * 24 - 6

		Lighting.GeographicLatitude = geoLatitude
		Lighting.ClockTime = clockTime % 24	

		-- Adjust brightness based on distance
		Lighting.Brightness = 500 / math.sqrt((closest.Position - cam.CFrame.Position).Magnitude)
	end
end)
1 Like

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