The title explains it all, how can I do that?
How to detect if a part is in a shadow (I’m not very sure)
One possible way to detect if a part is in a shadow is to use the FindPartOnRay method of the Workspace object1. This method takes a Ray object as an argument and returns the first part that intersects with the ray, or nil if there is none. You can use this method to cast a ray from your part to the sun direction and see if there is any other part blocking it.
To get the sun direction, you can use the GetSunDirection method of the Lighting object2. This method returns a unit vector pointing from the center of the world to where the sun is located. You can use this vector as the direction of your ray.
To create a Ray object, you need two arguments: an origin point and a direction vector3. The origin point can be any point on your part, such as its position or center. The direction vector can be obtained from the GetSunDirection method. You can also multiply this vector by a large number to make sure your ray reaches far enough.
Here is an example script that detects if a part named “MyPart” is in a shadow and prints it to the output:
local lighting = game:GetService("Lighting") local myPart = workspace:FindFirstChild("MyPart") if myPart then -- Get sun direction local sunDirection = lighting:GetSunDirection() -- Create ray from part position to sun direction local rayOrigin = myPart.Position local rayDirection = sunDirection * 1000 -- Multiply by 1000 for long distance local ray = Ray.new(rayOrigin, rayDirection) -- Find first part that intersects with ray local partFound = workspace:FindPartOnRay(ray) -- Check if part found is different from my part if partFound and partFound ~= myPart then print("Found something blocking sun:", partFound) print("MyPart is in shadow") else print("No shadow") print("MyPart is not in shadow") end end -- Connect function to TimeOfDay property change event of lighting lighting:GetPropertyChangedSignal("TimeOfDay"):Connect(function() -- Update sun direction when time of day changes sunDirection = lighting:GetSunDirection() end)
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.