Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity 2018 Artificial Intelligence Cookbook

You're reading from   Unity 2018 Artificial Intelligence Cookbook Over 90 recipes to build and customize AI entities for your games with Unity

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher
ISBN-13 9781788626170
Length 334 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Jorge Palacios Jorge Palacios
Author Profile Icon Jorge Palacios
Jorge Palacios
Jorge Elieser P Garrido Jorge Elieser P Garrido
Author Profile Icon Jorge Elieser P Garrido
Jorge Elieser P Garrido
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Behaviors - Intelligent Movement 2. Navigation FREE CHAPTER 3. Decision Making 4. The New NavMesh API 5. Coordination and Tactics 6. Agent Awareness 7. Board Games and Applied Search AI 8. Learning Techniques 9. Procedural Content Generation 10. Miscellaneous 11. Other Books You May Enjoy

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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime