How to detect when player looks at sun

I’ve done a bit of looking around and haven’t found anything about this topic.

When the player looks at or near the sun, I want to blind the player (I’ll probably mess around with depth of field or blur properties)

The problem is, I don’t know how to detect when the player is looking at or near the sun. Is there a way to calculate the position of the sun using lighting properties like TimeOfDay and GeographicLatitude?

Do you want to detect when the camera looks at the sun, or when the character’s head looks at the sun?

When the camera looks at the sun. It’s for a first person shooter

Add a new script into StarterPlayerScripts, and paste this inside:

--//Services
local Lighting = game:GetService("Lighting")

--//Variables
local CurrentCamera = workspace.CurrentCamera

--//Controls
local theta = 20
theta = math.cos(math.rad(theta))

--//Loopds
while task.wait(0.5) do
	local dirSun = Lighting:GetSunDirection()
	local dirCamera = CurrentCamera.CFrame.LookVector
	
	if dirSun:Dot(dirCamera) >= theta then
		print("Player is looking at sun")
		
		--//Run code
	else
		print("Player is not looking at sun")
	end
end

You can use :Dot to check the angle between the camera’s LookVector and the sun’s direction.

2 Likes

You can use sunrays in lighting.

Remember to give credit where credit is due.

2 Likes