How to check if an npc can see another npc

Hello! If the title sounds weird, let me explain.

I have an npc that uses a rocket launcher, and I want it to start shooting at another npc if that npc is in front of it (when I say in front, I mean whether the shooter npc is facing the other npc or not. Distance doesn’t matter). Currently, I use raycasting from the shooter npc to check if the other npcs are in view of the shooter. However, using raycasts only limits the view to that single line. If I wanted a wider view, how would I do that? (Hope I won’t have to fire multiple raycasts from the left end of the view to the right end of the view to achieve that)

1 Like

If you’re only using the raycast to check if the npc can see the other npc, you could just have it send a raycast directly towards the other npc and then check if it collides with anything. And if you want to make it only see the other npc if it’s in front, you can just ignore the raycast if the other npc is located behind the npc. I can give code snippets if you’d like, I also recently made some code that checks if a part is located in front or behind another part, though it’s not coded perfectly.

Also if you want to check for multiple npcs you can just loop through all of them and send a raycast for each individual one.

1 Like

Oop i may not have made it clear enough, sorry. The shooter npc is locked facing a specific direction, so it will never turn. Also there will be multiple npcs (they are enemies) here and there, so i dont want to run through all of them in a loop.

Begin by calculating the direction towards the other NPC. Once you have this, calculate the dot product of the aggressor NPC’s look-vector and that direction. The dot product of two vectors is a measure of how parallel they are. The range of that value is [-1, 1], where -1 is parallel in the opposite direction, 1 is parallel in the same direction, and 0 is perpendicular in both directions.

Since you only care for those in front of the aggressor NPC, you’ll want to look at range [0, 1]. I doubt you’ll want the aggressor NPC to have a FOV of 90° though, so [0.25, 1] may be more appropriate as it gives your NPC a FOV of 22.5°

local function inSightOf(aggressor: NPC, npc: NPC): boolean
    local aggressorPivot = aggressor:GetPivot()
    local npcPivot       = npc:GetPivot()
    local npcDirection   = (npcPivot.Position - aggressorPivot.Position).Unit

    return aggressorPivot.LookVector:Dot(npcDirection) >= PARALLEL_THRESHOLD
end

Where PARALLEL_THRESHOLD is the lower bound of the previously discussed appropriate ranges, for example, 0.25.

You cannot avoid iterating through all NPCs as you must check which ones are supposedly in sight with the function above. If the function passes, you can cast a ray to ensure no visually obstructive obstacles are present

Radius system (however this will usually result in getting all nearby instances depending how you do it) via studs or use raycasts from the head on wards however you will need to make sure the raycast hits the target.

…maybe i am still not clear enough, sorry. Your method calculates if the enemy npcs are within a cone shaped view of shooter npc. I want to calculate if the enemy npcs are within a rectangular view of the shooter npc.

Just make a part called hitBox and get all the parts inside of it. Ao something like his:

local hitBox = [HITBOX HERE]

local parts = hitBox:GetPartsBoundsInBox()
for _, part in pairs(parts) do
if part.Parent:FindFirstChild(“Humanoid”) then

[CODE HERE]
end
end

Block casting

local function inSightOf(aggressor: NPC, npc: NPC): boolean
    local aggressorPivot = aggressor:GetPivot()
    local npcPivot       = npc:GetPivot()
    local npcOffsetAbs   = (npc.Position - aggressor.Position):Abs()

    return npcOffsetAbs.X <= LENGTH and npcOffsetAbs.Z <= WIDTH
end

Block casting seems to be the solution I’m looking for! However, I’m not sure how much lag it will generate, because the server would be blockcasting every Heartbeat. Is there a less laggier alternative, or does it not even create that much lag at all (There may be multiple shooter npcs)

If you want to check if they see eachother block casting might be expensive and you should rather use a modified fov calculation but I think it’s worth a try. After all the best way to learn is to experiment.