Artificial Intelligence for Games

Share:

Unity's Artificial Intelligence tools offer many options to enhance your game's mechanics and bring a high degree of interactivity between the player and the game elements. This tutorial will explore a few key areas of Unity's AI capabilities, including NavMesh navigation, finite state machines, and behavior trees.

The Unity engine provides an integrated navigation feature called NavMesh, which creates a mesh terrain for your game characters to navigate. This includes pathfinding and it gives the ability for characters to move around complex maps. Consider an example of "JamesBond" who needs to chase "Villain" through a cityscape in your game.

The first step to use NavMesh is generating it:

using UnityEngine.AI;

public class NavMeshBaker : MonoBehaviour
{
  public NavMeshSurface surface;

  void Start()
  {
    surface.BuildNavMesh();
  }
}

In this example, NavMeshSurface is a component attached to an empty GameObject that represents our surfaces or 'paths' for our characters. Next, we need to adjust our AI to understand this NavMesh:

using UnityEngine;
using UnityEngine.AI;

public class JamesBondMovement: MonoBehaviour
{
  public Transform villainPosition;
  private NavMeshAgent agent;

  void Start()
  {
    agent = GetComponent<NavMeshAgent>();
    agent.SetDestination(villainPosition.position);
  }
}

In this script, GetComponent<NavMeshAgent>() retrieves the NavMeshAgent component, which contains information on AI locomotion and steering. The SetDestination(villainPosition.position) instructs each character to move towards the target destination.

Finite State Machines

Finite state machines (FSMs) is a basic and widely used AI concept. In essence, it defines an AI in terms of 'states' and the transitions between them. For example, in a game, "Thor" might have states like "Patrol", "Chase", and "Attack".

Here is a simple example of an FSM implementation for "Thor":

public class ThorAI : MonoBehaviour {
  enum State { Patrolling, Chasing, Attacking }
  State currentState;

  void Start() {
    currentState = State.Patrolling;
  }

  void FixedUpdate() {
    switch (currentState) {
    case State.Patrolling:
      // Patrol logic here...

      if (SeesPlayer()) {
        currentState = State.Chasing;
      }
      break;
    case State.Chasing:
      // Chase logic here...

      if (InAttackRange()) {
        currentState = State.Attacking;
      }
      else if (!SeesPlayer()) {
        currentState = State.Patrolling;
      }
      break;
    case State.Attacking:
      // Attack logic here...

      if (!SeesPlayer()) {
        currentState = State.Patrolling;
      }
      else if (!InAttackRange()) {
        currentState = State.Chasing;
      }
      break;
    }
  }
}

In this script, Thor's initial state is patrolling. If he sees the player, he will start chasing, and if the player is near enough, he will attack. If at any point, the player is not visible, Thor will go back to patrolling.

Behavior Trees

Behavior trees in artificial intelligence are tree-like models of structures used for decision-making. Let's consider a situation where a character, "Elsa" in our game, may have actions such as 'search for enemy', 'attack if enemy is in range' or 'run and heal if health is low'.

Here's a high-level code snippet to outline this:

public class ElsaAI : MonoBehaviour 
{
  void FixedUpdate() 
  {
    if(isHealthLow()) 
    {
      runAndHeal();
    } 
    else if(isEnemyInRange()) 
    {
      attack();
    } 
    else 
    {
      searchForEnemy();
    }
  }
}

This rudimentary implementation of a Behavior Tree makes Elsa run and heal if her health is low. If her health is fine and there's an enemy in the range, she will attack. Otherwise, she will search for enemies.

The unity AI tools offer so much more than pathfinding, finite state machines, and behavior trees. When combined, these tools can help you to create a more immersive and dynamic gaming experience. We have used uncomplicated examples in this tutorial to provide an understandable introduction to these concepts. With experience, you'll discover modern, complex and more efficient implementations. Towards this end, we hope that the understanding you gained through following this tutorial motivates you to independently learn more complex concepts, better practices, and advanced AI techniques.

0 Comment


Sign up or Log in to leave a comment


Recent job openings