Book Image

Unity 5.x Game AI Programming Cookbook

By : Jorge Palacios
5 (1)
Book Image

Unity 5.x Game AI Programming Cookbook

5 (1)
By: Jorge Palacios

Overview of this book

Unity 5 comes fully packaged with a toolbox of powerful features to help game and app developers create and implement powerful game AI. Leveraging these tools via Unity’s API or built-in features allows limitless possibilities when it comes to creating your game’s worlds and characters. This practical Cookbook covers both essential and niche techniques to help you be able to do that and more. This Cookbook is engineered as your one-stop reference to take your game AI programming to the next level. Get to grips with the essential building blocks of working with an agent, programming movement and navigation in a game environment, and improving your agent's decision making and coordination mechanisms - all through hands-on examples using easily customizable techniques. Discover how to emulate vision and hearing capabilities for your agent, for natural and humanlike AI behaviour, and improve them with the help of graphs. Empower your AI with decision-making functions through programming simple board games such as Tic-Tac-Toe and Checkers, and orchestrate agent coordination to get your AIs working together as one.
Table of Contents (15 chapters)
Unity 5.x Game AI Programming Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Blending behaviors by priority


Sometimes, weighted blending is not enough because heavyweight behaviors dilute the contributions of the lightweights, but those behaviors need to play 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

The 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' 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;
  2. Add the member variable for the minimum steering value to consider a group of behaviors:

    public float priorityThreshold = 0.2f;
  3. Add the member variable for holding the group of behavior results:

    private Dictionary<int, List<Steering>> groups;
  4. Initialize the variable in the Start function:

    groups = new Dictionary<int, List<Steering>>();
  5. 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();
    }
  6. 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);
    }
  7. 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;
            }
    }

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 least-priority group is chosen.

There's more...

We could extend this approach by mixing it with weighted blending; in this way, we would have a more robust architecture by getting extra precision on the way the behaviors make an impact on the agent in 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.