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)

Avoiding agents

In crowd-simulation games, it would be unnatural to see agents behaving entirely like particles in a physics-based system. The goal of this recipe is to create an agent capable of mimicking our peer-evasion movement.

Getting ready

We need to create a tag called Agent and assign it to those game objects that we would like to avoid, and we also need to have the Agent script component attached to them:

Example of how the Inspector window of a dummy agent should appear

Take a look on the following:

  • Tag: Agent (created by us)
  • Agent component is attached (the one created by us)

How to do it...

This recipe will entail creating a new agent behavior:

  1. Create the AvoidAgent behavior, which is composed of a collision avoidance radius and the list of agents to avoid:
using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 
 
public class AvoidAgent : AgentBehaviour 
{ 
    public float collisionRadius = 0.4f; 
    GameObject[] targets; 
} 
  1. Implement the Start function in order to set the list of agents according to the tag we created earlier:
void Start () 
{ 
    targets = GameObject.FindGameObjectsWithTag("Agent"); 
} 
  1. Define the GetSteering function:
public override Steering GetSteering() 
{ 
    // body 
} 
  1. Add the following variables to the compute distances and velocities from agents that are nearby:
Steering steering = new Steering(); 
float shortestTime = Mathf.Infinity; 
GameObject firstTarget = null; 
float firstMinSeparation = 0.0f; 
float firstDistance = 0.0f; 
Vector3 firstRelativePos = Vector3.zero; 
Vector3 firstRelativeVel = Vector3.zero; 
  1. Find the closest agent that is prone to collision with the current one:
foreach (GameObject t in targets) 
{ 
    Vector3 relativePos; 
    Agent targetAgent = t.GetComponent<Agent>(); 
    relativePos = t.transform.position - transform.position; 
    Vector3 relativeVel = targetAgent.velocity - agent.velocity; 
    float relativeSpeed = relativeVel.magnitude; 
    float timeToCollision = Vector3.Dot(relativePos, relativeVel); 
    timeToCollision /= relativeSpeed * relativeSpeed * -1; 
    float distance = relativePos.magnitude; 
    float minSeparation = distance - relativeSpeed * timeToCollision; 
    if (minSeparation > 2 * collisionRadius) 
        continue; 
    if (timeToCollision > 0.0f && timeToCollision < shortestTime) 
    { 
        shortestTime = timeToCollision; 
        firstTarget = t; 
        firstMinSeparation = minSeparation; 
        firstRelativePos = relativePos; 
        firstRelativeVel = relativeVel; 
    } 
} 
  1. If there is one that is prone to collision, then move away:
if (firstTarget == null) 
    return steering; 
if (firstMinSeparation <= 0.0f || firstDistance < 2 * collisionRadius) 
    firstRelativePos = firstTarget.transform.position; 
else 
    firstRelativePos += firstRelativeVel * shortestTime; 
firstRelativePos.Normalize(); 
steering.linear = -firstRelativePos * agent.maxAccel; 
return steering; 

How it works...

Given a list of agents, we take into consideration which one is closest, and, if it is close enough, we make it so that the agent tries to escape from the expected route of that first one according to its current velocity, so that they don't collide.

There's more...

This behavior works well when combined with other behaviors using blending techniques (some are included in this chapter); otherwise, it's a starting point for your own collision avoidance algorithms.