Sight for an enemy ai

Hello, im trying to make a game where an enemy roams around then starts to chase you if it spots you, however im unsure how to make the enemy able to lose sight of the player without it being too easy to lose sight or too hard. (my previous system made enemies easy to lose by going in circles around 1 object)

1 Like

yeah this one trips up basically everyone, it’s not really a raycast problem. your sight check is a true/false, so the second the ray hits the pillar the enemy just forgets you exist. one frame of cover and you’re gone.

what you want is awareness as a number instead of a flag. can see you = number goes up, can’t = number goes down. chase when it hits 100, stop chasing when it drops under like 45 (not 100, or it’ll flicker in and out).

if canSee then
    awareness = math.min(100, awareness + 60 * dt)
    lastSeen = os.clock()
    lastPos = root.Position
    lastVel = root.AssemblyLinearVelocity
elseif os.clock() - lastSeen > 3 then
    awareness = math.max(0, awareness - 20 * dt)
end

when you circle a pole you’re only out of sight for like half a second per lap, so the meter never even moves.

then when it does drop, don’t snap back to patrol. send them to lastPos + lastVel * 1 and let them poke around for a few seconds. they cut the corner instead of following your exact path, so circling just walks you straight into them lol

couple extras if it’s still too easy: footstep noise bumping awareness even with no line of sight.

It’s not really “sight” that you’re talking about, but giving the enemy object permanence and some simple heuristics for guessing where the player went after losing line of sight. Normally, you wouldn’t make a chasing NPC give up instantly, but rather have them continue to where they last saw the player, and remember which way they last saw that player heading. If, they can’t re-acquire line of sight within some period of time, or they see another target, they can give up chase. If the player turned a corner to break line of sight, and the NPC finds that, upon turning the same corner, they cannot see the player have have a choice of which way to go, they can just flip a coin (use RNG), and go in one possible direction until their attention span timer times out.

Depending on what mechanics make sense for your game, you can give NPCs other senses too, besides sight. Maybe they can “hear” sprinting players (acquiring their position, just as if seen) within some radius. Of maybe they see some past positions of the player (like where they were in over last second) if you want to simulate the NPC being able to track players as if they can smell them, or see footprints, etc.

Even more sophisticated NPCs might have knowledge of the environment, and be able to reason about where a player might be headed (i.e. location of some objective) and use pathfinding to cut them off.

There are lots of ways you can give NPCs more awareness that are more than line of sight but less than just total omniscience (like if they had total x-ray vision).

give them object permanence - when sight is lost, snapshot the players position and make them walk towards that. if can see player, continue chase, if cant, go into roam mode. that’s what id do anyway, dont take it for gospel ofc. test yourself first

sorry guys i forgot about this post lol, i already remade the entire enemy ai script and i have a way better sight function

if the player is within a very small distance, the enemy updates position (even through walls), but within a bigger distance they must raycast to see if they are in sight, if the player is outside the bigger distance or didnt get raycasted then the position doesnt update, the enemy gets 6 chances to find them before the chase ends