I am developing a horror game where the survivors have to hide and survive from a monster. If a monster is in contact with a player or facing towards a player within a distance, it will trigger a warning or whatever to that specific survivor to run. This is based on PvP (not PvE) teams.
I have done research of Raycasting and I still don't understand about it. I would appreciate if anyone explain or sample a code for me.
This is my code.
local Camera = workspace.CurrentCamera local part = workspace.Part local detection = Camera:WorldToScreenPoint(part.Position) if detection then for _, player in pairs(game.Players:GetPlayers()) do player.TeamColor = BrickColor.new("Black") script.Parent.Sound.Playing = true print("I see you!") end end
Your script currently has nothing to do with RayTracing and it seems like the goal of your script is to see if the player is visible or not. and right now your script isn't achieving your goal. and if you want find the player in different team then just do:
if player.TeamColor ~= game.Players.LocalPlayer.TeamColor then end
Now i'm fixing your script:
Your Script:
local Camera = workspace.CurrentCamera local part = workspace.Part local detection = Camera:WorldToScreenPoint(part.Position) if detection then for _, player in pairs(game.Players:GetPlayers()) do player.TeamColor = BrickColor.new("Black") script.Parent.Sound.Playing = true print("I see you!") end end
you should have two variables to Camera:WorldToScreenPoint():
local detection,isVisible = Camera:WorldToScreenPoint(part.Position)
then in your original script in the If Then Statement change detection to isVisible:
local Camera = workspace.CurrentCamera local part = workspace.Part local detection,isVisible = Camera:WorldToScreenPoint(part.Position) if isVisible then for _, player in pairs(game.Players:GetPlayers()) do player.TeamColor = BrickColor.new("Black") script.Parent.Sound.Playing = true print("I see you!") end end
How the script works should be self-explanatory now, right? because the isVisible variable comes after detection. And isVisible should be self-explanatory. But the problem with what you're trying to accomplish is that, every part that is within the Camera will make isVisible true even if you can't see the part because a wall is in the way.. To detect if the monster is looking at the player. You can use GetPartsObscuringTarget on Heartbeat to check if there are any parts obscuring the monster's view and if there are none then the monster is in fact, staring at the player.
EDIT: Hi, I'm back. It's been an hour after posting this Answer. I have been trying to Detect All Players In FOV for two days now. And I created a script that made it possible to detect player's in FOV. I won't give you the code but I'll give 3 steps and you will have to figure out the rest. Good Luck!
(1)Local Script
(2)WorldToScreenPoint
(3)GetPartsObscuringTarget
EDIT: Sure. All the code I had was in local script. I used WorldToScreenPoint to detect all visible parts even through walls. Then I used GetPartsObscuringTarget to detect if there are any parts blocking the local player's view from the part and if there are not any parts blocking the view then print("Player sees Part")
I still don't understand and sorry if I am asking too much. I really suck at this, but here's my code:
local Camera = workspace.CurrentCamera local part = workspace.part local castPoints = {part} local ignoreList = {} local detection, isVisible = Camera:WorldToScreenPoint(part.Position) local visibleTrue = Camera:GetPartsObscuringTarget(castPoints, ignoreList) if isVisible then if visibleTrue then print("Player sees Part.") else print("The part is no longer visible.") end end