I’m trying to make an enemy (that already has a pathfinding script) that detects the nearest player and chases them. The problem is that it detects players that are behind a barrier (or wall). I just want to know how to make it so it doesn’t detect players that it cant reach.
Edit: In this example i want the Enemy to not detect Player1 since it cant get to it, but still want it to detect Player2. Even though its behind a wall, its still within reach.
So an example for the Npc Enemy would be:
local rayOrigin= --write enemy npc is primparypart position or GetPivot() position !Make sure not a Cframe
local rayDestination=–this would be a player’s primparypart position or GetPivot() position !Make sure not a Cframe
local Raydirection=(rayDestination-rayOrigin).Unit–were going to subtract rayDestination-Rayorigin get a direction then we want to convert i to a unit ray to manipulate how many studs we want the ray to extend to its max limit e.g. the length/magnitude of ray could be 100 but there is a limit to how long the ray can be.
local raylength=150
local raycastResult=game.Workspace:Raycast(rayOrigin,rayDirection*raylength,raycastparams)
–The raycastparams are useful for excluding other objects such as other enemy npcs fom being detected and itself from the raycast operation
you should put your code into a block because this looks very unorganized and sharp to look at
local rayOrigin= --write enemy npc is primparypart position or GetPivot() position !Make sure not a Cframe
local rayDestination= --this would be a player’s primparypart position or GetPivot() position !Make sure not a Cframe
local Raydirection=(rayDestination-rayOrigin).Unit --were going to subtract rayDestination-Rayorigin get a direction then we want to convert i to a unit ray to manipulate how many studs we want the ray to extend to its max limit e.g. the length/magnitude of ray could be 100 but there is a limit to how long the ray can be.
local raylength=150 -- length of the ray, you can customize it
local raycastResult=game.Workspace:Raycast(rayOrigin,rayDirection*raylength,raycastparams)
--The raycastparams are useful for excluding other objects such as other enemy npcs fom being detected and itself from the raycast operation