I’ve seen games like ‘Shadow run’ and the point of the game is to jump on the shadows. How do you get a callback when a player touches a shadow? is it an invisible beam that the game used?
I think the closest way to make this is to detect if player is not getting pointed by the sunlight. Here is the script that I used to check if they are not getting pointed by sunlight. Create a LocalScript
inside StarterCharacterScript
.
local rayDirection = game.Lighting:GetSunDirection()
local char = game.Players.LocalPlayer.Character
local RAY_LENGTH = 100
game:GetService("RunService"):BindToRenderStep("SunService",Enum.RenderPriority.Camera.Value + 1,function()
local ray = Ray.new(char.HumanoidRootPart.Position,rayDirection * RAY_LENGTH)
local partFound = workspace:FindPartOnRay(ray,char)
if partFound then
print("Touched shadow!")
end
end)
game.Lighting:GetPropertyChangedSignal("TimeOfDay"):Connect(function()
rayDirection = game.Lighting:GetSunDirection()
end)
Also this will base on the sun direction so you will need to change it manually for it to work well.
I understand most of the parts, but can you explain 'BindToRenderStep" ? I’ve never seen it before.
This is a small function of RunService
which are really helpful on making some kinds of physics like burn you in sunlight, get touching shadow, … Here is some information about it here.
I read that, yet I don’t understand it. Is it like coroutine?
It’s similar to RenderStepped
but you can call it at a specific time. At you can see in my script, the function :BindToRenderStep()
have 3 agruments. First is the name for the binding, next is the priority which is Enum.RenderPriority.Camera.Value + 1
. This mean it will run after the camera rendering. The last one is the function will be run which detects if player touching shadow or not. For easy, this will run the function every frame but with priority.
So it creates a ‘loop’ that can be stopped called ‘SunService’, but I don’t get the ‘priority’ part. Does it mean that it runs after the player camera is finished ‘moving’?
No, you understand a bit. Imagine your FPS is 60. So it will run 60 times every seconds. But the priority part here mean that it will run after the rendering. For easier, it will run like sequentially, which will run first and which will run after. Like it render the camera first after that run the function.
Meaning that it is just a while true loop (that can be stopped) that goes with the FPS so the game doesn’t crash? If that is correct then I think I get what this does.
Please use the workspace:Raycast
function, as the current 1 you are using is deprecated. It will require a bit of work to change.
It run every frame but sequentially. And you can please change ‘’’:FindPartOnRay()’’’ into ‘’’:Raycast()’’’? I just realized that my previous function has deprecated. It just need a bit change for it to work.
thank you! I get it now. Yes, I will change it to Raycast()