(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)
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)
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! ^^
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)