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

Blending behaviors by priority

Sometimes, weighted blending is not enough because heavyweight behaviors dilute the contribution of the lightweights, but those behaviors need to do their part too. That's when priority-based blending comes into play, applying a cascading effect from high-priority to low-priority behaviors.

Getting ready

This approach is very similar to the one used in the previous recipe. We must add a new member variable to our AgentBehaviour class. We should also refactor the Update function to incorporate priority as a parameter to the Agent class's SetSteering function. The new AgentBehaviour class should look something like this:

public class AgentBehaviour : MonoBehaviour 
{ 
    public int priority = 1; 
    // ... everything else stays the same 
    public virtual void Update () 
    { 
        agent.SetSteering(GetSteering(), priority); 
    } 
} 

How to do it...

Now, we need to make some changes to the Agent class:

  1. Add a new namespace from the library:
using System.Collections.Generic; 
  1. Add the member variable for the minimum steering value to consider a group of behaviors:
public float priorityThreshold = 0.2f; 
  1. Add the member variable for holding the group of behavior results:
private Dictionary<int, List<Steering>> groups; 
  1. Initialize the variable in the Start function:
groups = new Dictionary<int, List<Steering>>(); 
  1. Modify the LateUpdate function so that the steering variable is set by calling GetPrioritySteering:
public virtual void LateUpdate () 
{ 
    //  funnelled steering through priorities 
    steering = GetPrioritySteering(); 
    groups.Clear(); 
    // ... the rest of the computations stay the same 
    steering = new Steering(); 
} 
  1. Modify the SetSteering function's signature and definition to store the steering values in their corresponding priority groups:
public void SetSteering (Steering steering, int priority) 
{ 
    if (!groups.ContainsKey(priority)) 
    { 
        groups.Add(priority, new List<Steering>()); 
    } 
    groups[priority].Add(steering); 
} 
  1. Finally, implement the GetPrioritySteering function to funnel the steering group:
private Steering GetPrioritySteering () 
{ 
    Steering steering = new Steering(); 
    float sqrThreshold = priorityThreshold * priorityThreshold; 
    foreach (List<Steering> group in groups.Values) 
    { 
        steering = new Steering(); 
        foreach (Steering singleSteering in group) 
        { 
            steering.linear += singleSteering.linear; 
            steering.angular += singleSteering.angular; 
        } 
        if (steering.linear.sqrMagnitude > sqrThreshold || 
                Mathf.Abs(steering.angular) > priorityThreshold) 
        { 
            return steering; 
        } 
    } 
    return steering; 
}

How it works...

By creating priority groups, we blend behaviors that are common to one another, and the first group, in which the steering value exceeds the threshold, is selected. Otherwise, steering from the lowest-priority group is chosen.

There's more...

We could extend this approach by mixing it with weighted blending, so we would have a more robust architecture, achieving extra precision in the way the behaviors impact on the agent at every priority level:

foreach (Steering singleSteering in group) 
{ 
    steering.linear += singleSteering.linear * weight; 
    steering.angular += singleSteering.angular * weight; 
} 

See also

There is an example of avoiding walls using priority-based blending in this project.

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