Swinging a ray in a Cone in front of the player is often many times faster than using many rays. also makes your AI look like it takes a moment to react and other times reacts instantly
You should have illustrated the single ray problem with the Half-Life 2 example, where the player “hid” from an enemy by holding a can in front of their own eyes 😅
An even cheaper trick: Make that single ray slightly swing from side to side when it's not seeing the player.
a cheaper method is to use fewer rays, but introduce randomization into the direction within a certain tolerance, and have this poll maybe every 1/5 of a second or so. this way you might be 'visible' but they don't immediately notice you, more like a real person or animal. less code, less system resources, more realistic performance. win all around.
There’s an even cooler technique I once saw in a talk about the battlefield engine. They basically render a simplified and scaled down image of the environment with a camera from the perspective of an ai actor, rendering black all that covers and white the player. If enough white pixels set by a threshold are visible on the image, the ai basically „sees“ the player/enemy. This made it possible to let them see through dynamically created cracks/holes in walls, broken fences and so on. A bit more expensive in resources, but makes up with credible ai.
The AI knows where the player is, because it knows where the player isn't
I'd love to poke around in a bare bones demo project both in 2D and 3D as to how this works exactly. Or a video that goes more into depth on this subject in Godot :)
Stealth mechanics in games are so interesting to me because the AI needs to be programmed to pretend it's not aware of the player in a way that convinces the player they're hidden, without making them feel like they're not responsible for being so sneaky The way developers need to integrate enemy ai and level design to achieve this effect is one of the most satisfying things in gaming when pulled off.
I'm working on an infiltration game. I found out that putting target on the bones on the player character and making the AI cast rays to those target when in range and checking if the target are in the FOV of the Ennemis, is way more efficient for precise detection than just trow a bunch of rays in the player direction. I've even put scores on each Target to make each Target impact the detection rate differently !
So in otherwords, the AI DOES always know exactly where you are 💀
Don’t forget to add coyote timer for the ai and extend its vision length quite a bit when it senses player
Another way to optimize this is to stop firing rays once the player is too far from the max distance, so when you have like more than 10 enemies the game doesn't lag at all (unless they're all like. crammed or something)
I like to use a trigger zone. If the player is inside that, the game casts a ray to the player and fifteen more randomized in a cone around that ray. If more than 3 of those rays hit, the AI gets "suspicious". If more than 10 hit, the AI "sees" you. That way, if the AI "sees" you from above or from a difficult angle, you're harder to spot. Also helps if you crouch while further away.
In The Legend of Zelda: Majora's Mask, when infiltrating the Deku Palace there is a stealth section where the camera is positioned for a top-down view. At night, the patrolling Deku guards actually project a line of dots to illustrate their ray of vision.
AAA dev here, I work on an FPS brand on the AI team! For our vision tests we use key points on the player's model for testing, specifically the head, chest, both hands and both feet! Since we're first person we don't need higher fidelity than that, but it still covers enough cases to work as expected :)
the problem arises if the ray accidentally overshoots the object. you could fix the problem by checking for each point in a line, but that would be slow. maybe looping through all outer points of the objec tindtead would be better?
This is the only #Shorts on Youtube I've appreciated, not even lying. Great job, more of these!
Instead of many rays at once you can be checking one random point of the player each frame so the more visible you are the more likely you are to be seen.
Some games do this by giving the AI a camera. The camera works the same as in Wolfenstein 3d; drawing "walls" based on distance from the entity. These walls are defined by two things. The 5 corners of the view-cone and, the amount of time defined by the programmer.
@z-beeblebrox