Tags

, ,

I’ve been working on the AI for Spamocalypse in my free time, and I have the basic detection mechanisms done. Here’s how they work.

    Line of Sight

Line of sight detection in Unity is something I’ve long since got the hang of. My usual method is to create a 3D shape, import it into Unity, and then attach a Mesh Collider with the “isTrigger” attribute marked in the Inspector Panel. When an object such as the player enters that trigger volume, a ray is cast towards the object. If the ray hits something other than the object, then the AI unit won’t have a line of sight to the object; however, if it does, then the AI unit will begin a counter to decide if the player is actually the player. If that counter reaches 50, it informs the brain, which will cause the AI to investigate; whereas if the counter reaches 100, then it has definitely spotted the player.
There is one problem with the above setup: the line runs from the AI to the centre of the player. If the player’s centre is just hidden behind a wall, but they are still visible peering around the corner, the AI won’t register them as visible yet. I’ll need to figure this out, but the core concept works.

    Sound

Sound detection was a little harder, simply because I’d never done it before. However, I’ve got it working as follows: a sphere collider is attached to a child object of the AI unit. That too is set to act as a trigger. When the player enters the trigger volume, it begins to calculate the player’s noise using the following formula:

noise = player_Speed * player_Footsteps_Volume / distance_From_Agent

That’s calculated on the physics timestep, which by default is every 50th of a second. If the noise on the current timestep is greater than it was on the previous timestep, the detection increases by a specific amount. If the detection reaches 50, the agent begins to investigate. If it reaches 100, then it has definitely spotted the player, and will begin to attack.
The sound detection will also activate for decoys. When a decoy enters the trigger volume, the agent checks if it’s playing. When it starts playing, the sound detection script informs the brain. If the agent hasn’t had enough false alerts, it will investigate the decoy. During the initial investigation, they will ignore the player.

Below is a screencap of the two in the game world. The semi-transparent blue sphere is the collider for sound detection, while the collider for line of sight is a flat green rectangle with one end wider than the other. I’ve tested these, and they both work, albeit rather slowly due to the low detection increments. Once I have player movement finished, I’ll create a demo version for people to try out.

Detection Triggers

Advertisements