Unity 3D - Artificial Intelligence

The game will have an AI NPC patrolling the scenes, which will attack the player should they spot them. 

After downloading a model character from the asset store that fitted my game [1], it was time to implement intelligence. 












The first thing to do was bake a nav mesh to dictate where the AI can move. I stumbled across a big problem where only the connections of my world were baking, leaving huge gaps between them. With help from my supervisor, I realised that the objects not working had not got the navigation static option checked, meaning they were unable to be used for a nav mesh. 








After ticking them I was able to have a fully baked nav mesh for the tunnels scene. The blue areas show where the AI can move:









I then added a NavMeshAgent to the NPC and edited the details to match the model: 












With the initial navigation set up I then added two scripts from the Lynda.com tutorial mentioned earlier in the blog: "AI_Enemy" and "Line Sight". 
The AI_Enemy script introduces a finite state machine. It dictates whether the NPC is in a PATROL state, CHASE state, or ATTACK state. 



It also links to many destination points I placed around the map as seen here:







In total there are 18 Dest points in this scene. The script randomly selects one of those points and tells the NPC to patrol there, changing as time goes on. 
The states are decided on what the Line Sight script reveals. This script works with the sphere collider to decide where the NPC can look and whether or not it can see the player.





If the "Can See Target" Boolean is not checked, then the AI patrols, if it is checked, it either chases or attacks, depending on the distance from the player. In order for this to work I needed to create an empty game object labelled eyes to make sure the eyes were aligned correctly.










There is a health component on the player that works with the AI too:

This is a simple script that dictates the total health of the player, and reacts to the attack function of the AI_Enemy script. The reason for the player having 1 is that I want the capture to be instant. Once caught, a new scene loads. This is the Game Over screen where the player has the option to retry or exit to the main menu.







To make this screen a little more interesting the text fades in followed by the buttons.


When I first implemented the AI, no animation would play. The character model was stuck in the T-Pose and gliding across the floor. After a some time searching, I found that the problem was the Animation Controller, so I created a new controller where the states match the ones from the AI_Enemy script. Here is the animator with transitions:













Comments