Moving a star up and down depending on the players distance from it

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

5 Likes

I don’t think what you want to do is possible due to the trick you are playing by placing a distant sun behind a ‘close star’. Roblox lighting is making the sunset toward the horizon. It will not make a sunset at noon (when the sun is straight above you). You can’t go under the sun at sunset.(with the orange rays). You may have to try and tweak the lighting settings and try to get a more orange look at noon by tweening, but idk if that is possible.

The effect looks amazing though, disregarding the color shift, wow!

Only other option I can think of is make sure the star passes to the right or left horizon line and not above.

4 Likes

I do believe it is possible, Starscape managed to pull it off pretty well and I’ve gotten close to doing it.

Here’s what I mean: https://youtu.be/ew82o3Dke-Y?si=p9K568_6VqZi2ABm&t=69

Surely this is possible, I know it’d be hard and not perfect but surely it’s possible

2 Likes

If you listen at 2:30 seconds in the video you sent, he works around this horizon sunset ray color problem by keeping the sun low to the side as you pass it. He configued it to make a U shaped path(as he says) so that it stays low to the side to preserve the roblox sun ray colors.

3 Likes

Yes, that’s precisely what I’m trying to pull off. That would actually work perfectly fine for my purposes.

1 Like

Well make it so the star stays to the right or left and stays on the horizon position and you should be good do go.

2 Likes

I think I could pull that off, but how’d I determine what the horizon position needs to be? i.e how would I calculate what Y position the star would need to be in to keep the sun in the correct clocktime?

1 Like

I don’t know but I suspect you will have to tweak both the time and the geographic lattitude. Try setting the sun to 12 noon and lattitude to 100 and you’ll see what I mean

Yeah, this is what I’m changing to position the sun behind the star. The sun is moving in accordinance with the star. I just have to move the star down and to the left or right depending on how close the player is to it, and it should hopefully work. Hopefully.

Alterantively, if I could figure out the correct Y position at any given moment I can just… set the star to be that Y position

but how would I do that?

Bit of a necrobump but I finally found the solution.

1 Like

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