How would I check if the player is looking at the sun?

Hello, I work on a big game project and I wonder how would I check if the player is looking at the sun.

My idea here is to create a GUI effect of sunshine that pops up when you look at the sun.
I already created the effect. Now, I need to get it working. Any idea how?

1 Like

do you have a day/night cycle?

1 Like

Yes, I have a day/night cycle built in with the weather system.

Do SunRays not work for your use case?

And if not, I really wouldn’t suggest trying to get this working. I wouldn’t even know where to start.

It’s one of the many limitations of Roblox. There is no API for detecting the current position of the sun on the screen. I imagine you could hard code this with a lot of effort, but it would be approximate at best and be extremely fragile.

I do not recommend pursuing a custom implementation, at all.

See below. Forgot :GetSunDirection() exists.
You’ll need to familiarise yourself with dot products, though.

1 Like

Answering your question with the given context I would probably start with game.Lighting:GetSunDirection() and using :Dot

local sunDirection = game.Lighting:GetSunDirection()
local angle = 20
angle = math.cos(math.rad(angle))

game["Run Service"].RenderStepped:Connect(function(dt)

	local directionCamera = workspace.CurrentCamera.CFrame.LookVector
	
	if sunDirection:Dot(directionCamera) >= angle then
		print("woah im like, super looking at the sun")
	else
		print("depression i am not looking at the sun")
	end
end
3 Likes

Never heard of :Dot… Guess I’ll try it out.

Yes I do use SunRays but I just wanted to add a better effect.

Wow, thanks a lot! I struggled trying to get it right before. But, really thanks!

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