Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
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 FREE CHAPTER 2. Navigation 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

Arriving and leaving

Similar to Seek and Flee, the idea behind these algorithms is to apply the same principles and extend the functionality to a point where the agent stops automatically after a condition is met, either being close to its destination (arrive), or far enough from a dangerous point (leave).

Getting ready

We need to create one file for the Arrive and Leave algorithms respectively, and remember to set their custom execution order.

How to do it...

They use the same approach, but in terms of implementation, the name of the member variables change, as well as some computations in the first half of the GetSteering function:

  1. First, implement the Arrive behavior with its member variables to define the radius for stopping (target) and slowing down:
using UnityEngine; 
using System.Collections; 
 
public class Arrive : AgentBehaviour 
{ 
    public float targetRadius; 
    public float slowRadius; 
    public float timeToTarget = 0.1f; 
} 
  1. Create the GetSteering function:
public override Steering GetSteering() 
{ 
    // code in next steps 
} 
  1. Define the first half of the GetSteering function, in which we compute the desired speed depending on the distance from the target according to the radii variables:
Steering steering = new Steering(); 
Vector3 direction = target.transform.position - transform.position; 
float distance = direction.magnitude; 
float targetSpeed; 
if (distance < targetRadius) 
    return steering; 
if (distance > slowRadius) 
    targetSpeed = agent.maxSpeed; 
else 
    targetSpeed = agent.maxSpeed * distance / slowRadius; 
  1. Define the second half of the GetSteering function, in which we set the steering value and clamp it according to the maximum speed:
Vector3 desiredVelocity = direction; 
desiredVelocity.Normalize(); 
desiredVelocity *= targetSpeed; 
steering.linear = desiredVelocity - agent.velocity; 
steering.linear /= timeToTarget; 
if (steering.linear.magnitude > agent.maxAccel) 
{ 
    steering.linear.Normalize(); 
    steering.linear *= agent.maxAccel; 
} 
return steering; 
  1. To implement Leave, change the name of the member variables:
using UnityEngine; 
using System.Collections; 
 
public class Leave : AgentBehaviour 
{ 
    public float escapeRadius; 
    public float dangerRadius; 
    public float timeToTarget = 0.1f; 
} 
  1. Define the first half of the GetSteering function:
Steering steering = new Steering(); 
Vector3 direction = transform.position - target.transform.position; 
float distance = direction.magnitude; 
if (distance > dangerRadius) 
    return steering; 
float reduce; 
if (distance < escapeRadius) 
    reduce = 0f; 
else 
    reduce = distance / dangerRadius * agent.maxSpeed; 
float targetSpeed = agent.maxSpeed - reduce; 
  1. And finally, the second half of GetSteering stays just the same.

How it works...

After calculating the direction to go in, the next calculations are based on two radii distances in order to know when to go full throttle, slow down, and stop; that's why we have several if statements. In the Arrive behavior, when the agent is too far, we aim for full throttle, progressively slow down when inside the proper radius, and finally stop when close enough to the target. The inverse method applies to Leave:

A visual reference for the Arrive and Leave behaviors
You have been reading a chapter from
Unity 2018 Artificial Intelligence Cookbook - Second Edition
Published in: Aug 2018
Publisher:
ISBN-13: 9781788626170
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 $19.99/month. Cancel anytime
Banner background image