Hey. I’m having trouble with some code that makes the stars in my game work properly. It essentially entails moving the star vertically depending on how far away it is from the player (only on the X and Z axes, Y isn’t too important) in order to keep the sunrays a consistent color.
For the purposes of this forum post, the “star” refers to the thing circled in blue in the image above (the other 2 stars are moved with the main star, so the only thing we have to worry about is that highlighted star.). The “sun” refers to ROBLOX’s built-in sun, the sunrays are highlighted in red.
The system for moving the sun in accordinace with the star is already in place and functions fine. The issue with this arises when we want to move around the system, which because of the way it’s setup and the way that sunrays work, causes the issue of the sunrays changing colors depending on how close the player is to the star (demonstrated in the video below:)
robloxapp-20230901-1617339.wmv (2.3 MB)
As you can see, the sunrays change color as the player gets closer. I don’t want this to be the case, the sunrays should stay the same orange color consistently throughout the entire system, no matter where the player is. The way to accomplish this is to ensure that the star moves down closer to the player as they get closer to the star, that way the sunrays are always in such a position that they remain that orange color consistently.
My trouble is figuring out precisely how to do this. The methods I’ve tried have all either been too unreliable, too sporadic, or have flat out not worked.
This is the current method, which technically works but is really quite inconsistent and ugly.
function Calculations.ToLatLon(self: Calculations, direction: Vector3): (number, string) -- this function was written by another coder, although I do understand how it works. It's not the important piece here, but it adds valuable context.
direction = Vector3.new(-direction.X, -direction.Y, direction.Z)
local lat = atan2(direction.Z, sqrt(direction.X^2 + direction.Y^2))
local lon = atan2(direction.Y, direction.X)
lat = self.rad_sc(lat) * 360 + 23.5
lon = self.ToTimeOfDay(self.rad_sc(lon) * 24 - 6)
return lat, lon
end
local function positionCelestialBody(Planet:Instance, playerPos:Vector3, baseDistance:number, camPos:Vector3)
local planetPos = Planet:FindFirstChild("TruePosition").Value
local diff = (planetPos - playerPos)
local dist = diff.Magnitude
local scalar = (1 / dist) * baseDistance --math.clamp((1 / dist) * baseDistance, 0, 1)
local size
local visualPos = Vector3.new(
diff.X * scalar * kilometersToStuds + camPos.X,
diff.Y * scalar * kilometersToStuds + camPos.Y,
diff.Z * scalar * kilometersToStuds + camPos.Z
)
if Planet:FindFirstChild("Planet") then
-- not important
elseif Planet:FindFirstChild("Star 1") then
local sunPos = Planet:GetPivot().Position
local planeDist = Vector2.new(sunPos.X - playerPos.X, sunPos.Z - playerPos.Z).Magnitude
Planet:PivotTo(CFrame.new(visualPos))
for _,Star in pairs(Planet:GetChildren()) do
if Star.Name ~= "TruePosition" then
size = (Star:FindFirstChild("ScaleFactor", true).Value * 50) * scalar * kilometersToStuds
Star.Main.Size = Vector3.new(size,size,size)
end
end
else
-- also not important
end
end
This code is simply not sufficient for my purposes, it works to move the star laterally but it needs to be moved vertically so that it can stay consistent. Any help with this would be appreciated.
I can share any methods I’ve already tried if need be.
Thanks,
Alte