Book Image

Unity 2018 Artificial Intelligence Cookbook - Second Edition

By : Jorge Palacios
Book Image

Unity 2018 Artificial Intelligence Cookbook - Second Edition

By: Jorge Palacios

Overview of this book

Interactive and engaging games come with intelligent enemies, and this intellectual behavior is combined with a variety of techniques collectively referred to as Artificial Intelligence. Exploring Unity's API, or its built-in features, allows limitless possibilities when it comes to creating your game's worlds and characters. This cookbook covers both essential and niche techniques to help you take your AI programming to the next level. To start with, you’ll quickly run through the essential building blocks of working with an agent, programming movement, and navigation in a game environment, followed by improving your agent's decision-making and coordination mechanisms – all through hands-on examples using easily customizable techniques. You’ll then discover how to emulate the vision and hearing capabilities of your agent for natural and humanlike AI behavior, and later improve the agents with the help of graphs. This book also covers the new navigational mesh with improved AI and pathfinding tools introduced in the Unity 2018 update. You’ll empower your AI with decision-making functions by programming simple board games, such as tic-tac-toe and checkers, and orchestrate agent coordination to get your AIs working together as one. By the end of this book, you’ll have gained expertise in AI programming and developed creative and interactive games.
Table of Contents (12 chapters)

Pursuing and evading

Pursuing and evading are great behaviors to start with because they rely on the most basic behaviors and extend their functionality by predicting the target's next step.

Getting ready

We need a couple of basic behaviors called Seek and Flee; place them right after the Agent class in the scripts' execution order.

The following is the code for the Seek behavior:

using UnityEngine; 
using System.Collections; 
public class Seek : AgentBehaviour 
{ 
    public override Steering GetSteering() 
    { 
        Steering steering = new Steering(); 
        steering.linear = target.transform.position - transform.position; 
        steering.linear.Normalize(); 
        steering.linear = steering.linear * agent.maxAccel; 
        return steering; 
    } 
} 

Also, we need to implement the Flee behavior:

using UnityEngine; 
using System.Collections; 
public class Flee : AgentBehaviour 
{ 
    public override Steering GetSteering() 
    { 
        Steering steering = new Steering(); 
        steering.linear = transform.position - target.transform.position; 
        steering.linear.Normalize(); 
        steering.linear = steering.linear * agent.maxAccel; 
        return steering; 
    } 
}

How to do it...

Pursue and Evade are essentially the same algorithm, but differ in terms of the base class they derive from:

  1. Create the Pursue class, derived from Seek, and add the attributes for the prediction:
using UnityEngine; 
using System.Collections; 
 
public class Pursue : Seek 
{ 
    public float maxPrediction; 
    private GameObject targetAux; 
    private Agent targetAgent; 
} 
  1. Implement the Awake function in order to set up everything according to the real target:
public override void Awake() { base.Awake(); targetAgent = target.GetComponent<Agent>(); targetAux = target; target = new GameObject(); } 
  1. Implement the OnDestroy function for properly handling the internal object:
void OnDestroy () 
{ 
    Destroy(targetAux); 
} 
  1. Implement the GetSteering function:
public override Steering GetSteering() 
{ 
    Vector3 direction = targetAux.transform.position - transform.position; 
    float distance = direction.magnitude; 
    float speed = agent.velocity.magnitude; 
    float prediction; 
    if (speed <= distance / maxPrediction) 
        prediction = maxPrediction; 
    else 
        prediction = distance / speed; 
    target.transform.position = targetAux.transform.position; 
    target.transform.position += targetAgent.velocity * prediction; 
    return base.GetSteering(); 
} 
  1. To create the Evade behavior, the procedure is just the same, but it takes into account the fact that Flee is the parent class:
public class Evade : Flee 
{ 
    // everything stays the same 
} 

How it works...

These behaviors rely on Seek and Flee and take into consideration the target's velocity in order to predict where it will go next, and they aim at that position using an internal extra object.