Unity3D – Game Engine – Artificial Intelligence

What is AI?

Artificial Intelligence (AI) is essentially the subject of making computers able to think and decide like living organisms to perform specific operations. AI in videogame is the brain of NPC (Non Player Characters), the computer-driven characters.

AI setup

Our NPC opponents can be very stupid like a zombie or super intelligence as the chess game champion Kasparow but the important thing it is our game have to be funny! The NPC can not be invicible or too easy to win. Finding the right challenge level is the key to make a game fun to play.
There are a lot of techniques, so let’s go on!

Finite State Machines

It is the the simplest AI model.
A state machine basically consists of a finite number of states that are connected in a graph by the transitions between them.

NPC character -> It follows some rules -> It changes state

NPC character can only be in exactly one state at any given time.

Random and Probability

In videogames, randomness and probabilities are applied in the decision making process of AI calculations.

Stupid NPC
-> good way (30%) -> it is easy to beat it!
-> bad way (70%)

Genius NPC
-> good way (90%) -> run fast and escape!
-> bad way (10%)

Sensor System

Our NPCs need eyes and ears to know about enemies and world they are interacting with.
The basic informations the NPCs need could vary a lot depending on the type of the game, for example:

NPC in a RTS (Real Time Startegy) game:
– Position of the player
– Buildings and objects nearby
– Player’s health and its own health
– Location of resources on the map

NPC zombie:
– Position of the player (to eat him…) :)

Pooling

One method to collect the informations of the world is polling.
NPCs -> Pooling (if/else) -> Reaction

We can simply do if/else or switch checks in the FixedUpdate() method of our AI character.

The messaging system

NPC AI does decision making in response to the events in the world.
The messaging system provides a central checking system.

Event -> NPCs (listeners) -> Pooling -> Reaction

Flocking, swarming, and herding

Think about group of birds flocking in the sky. We can define simple rules for each bird to create the whole group behaviour.

There are 3 simple rules:
– Separation: minimum distance between neighbors
– Alignement: direction based on direction of the neighbors
– Cohesion : minimum distance with the group’s center of mass

Path following and steering

Think about a racing game, the AI opponents need to navigate on the road.

A* (star) Pathfinding

The AI units need to find a path to reach the goal without colliding with the obstacles or colliding with others NPC.
A* (pronounced “A star”) is a pathfinding algorithm widely used in games, because of its performance and accuracy.

A Navigation Mesh

A navigation mesh it is a poly mesh that uses polygons to represent the areas in the map that an AI entity can travel. It is the ‘road’ of our NPCs players. An NPC will knows more informations about world than a waypoint system as A* Pathfinding system. The AI character will knows the safe areas, if there is mud, grass, ice, water and so on…
Unity3D Pro has a built-in navigation mesh generator.

Behavior Trees

With a behavior tree you can represet the logic of an AI character.

NPC Action
|1.-> |Is Player faraway?
|Patrol (Ispeziona)
|2.-> |Do I see the Player?
|Chase (Raggiungi)
|3.-> |Is Player near enought to attack?
|Attack (Attacca)

Try to think as your NPC:
1. The Player is faraway -> Patrol |2. I see the Player -> Chase |3. I am near enough -> Attack
The NPC can not reach point 3. if do not reach point 2. first.
Point 3. it is the child behaviour of point 2.

Locomotor System

Locomotor system manages different NPC’s walk cycles to walk on plane, run, climb a ladder, attack an enemy etc…
Unity3D pro has a built-in locomotion system: Mecanim