Is it possible to detect sunlight hitting the player?

I’m making a jungle survival game. One thing I want to add is SPF/overheating if you’re out in the direct sunlight too long. Is it possible to detect sunlight/amount on the player?

I know I could make regions with blocks where the player doesn’t face the effects but the issue then comes to when players are under trees and such since the foliage covers them in certain spots depending on the time of the day and not others. Is it possible to figure out if and/or the amount of sunlight directly hitting the player?

1 Like

One thing that you could try is raytracing between the players head and an invisible part in the sky. If the ray successfully reaches the invisible part without bumping into anything else, the player is in the sun.

1 Like

By performing some kind of calculations on the longitude, lattitude and clocktime you can figure out the sunlight facing direction

EDIT:
Looks like there is a function in roblox API for it!!!
you can get the sun’s direction vector by doing the following:

local Lighting = game:GetService("Lighting")
local SunDirection = Lighting:GetSunDirection()

SunDirection will contain the sun’s direction from the origin of the world Vector3.new(0,0,0)
Then using raycasting in the player’s origin with the sun’s direction, you can figure out if the sunlight is hitting the player or not!

Reference:

6 Likes