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 5.x Game AI Programming Cookbook

You're reading from   Unity 5.x Game AI Programming Cookbook Build and customize a wide range of powerful Unity AI systems with over 70 hands-on recipes and techniques

Arrow left icon
Product type Paperback
Published in Mar 2016
Publisher Packt
ISBN-13 9781783553570
Length 278 pages
Edition 1st Edition
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 (10) Chapters Close

Preface 1. Behaviors – Intelligent Movement FREE CHAPTER 2. Navigation 3. Decision Making 4. Coordination and Tactics 5. Agent Awareness 6. Board Games AI 7. Learning Techniques 8. Miscellaneous Index

Creating the behavior template

Before creating our behaviors, we need to code the stepping stones that help us not only to create only intelligent movement, but also to build a modular system to change and add these behaviors. We will create custom data types and base classes for most of the algorithms covered in this chapter.

Getting ready

Our first step is to remember the update function order of execution:

  • Update
  • LateUpdate

Also, it's important to refresh so that we can select the scripts' order of execution. For our behaviors to work as intended, the rules for ordering are as follows:

  • Agent scripts
  • Behavior scripts
  • Behaviors or scripts based on the previous ones
Getting ready

This is an example of how to arrange the order of execution for the movement scripts. We need to pursue derives from Seek, which derives from AgentBehaviour.

How to do it...

We need to create three classes: Steering, AgentBehaviour, and Agent:

  1. Steering serves as a custom data type for storing the movement and rotation of the agent:
    using UnityEngine;
    using System.Collections;
    public class Steering
    {
        public float angular;
        public Vector3 linear;
        public Steering ()
        {
            angular = 0.0f;
            linear = new Vector3();
        }
    }
  2. Create the AgentBehaviour class, which is the template class for most of the behaviors covered in this chapter:
    using UnityEngine;
    using System.Collections;
    public class AgentBehaviour : MonoBehaviour
    {
        public GameObject target;
        protected Agent agent;
        public virtual void Awake ()
        {
            agent = gameObject.GetComponent<Agent>();
        }
        public virtual void Update ()
        {
                agent.SetSteering(GetSteering());
        }
        public virtual Steering GetSteering ()
        {
            return new Steering();
        }
    }
  3. Finally, Agent is the main component, and it makes use of behaviors in order to create intelligent movement. Create the file and its barebones:
    using UnityEngine;
    using System.Collections;
    public class Agent : MonoBehaviour
    {
        public float maxSpeed;
        public float maxAccel;
        public float orientation;
        public float rotation;
        public Vector3 velocity;
        protected Steering steering;
        void Start ()
        {
            velocity = Vector3.zero;
            steering = new Steering();
        }
        public void SetSteering (Steering steering)
        {
            this.steering = steering;
        }
    }
  4. Next, we code the Update function, which handles the movement according to the current value:
    public virtual void Update ()
    {
        Vector3 displacement = velocity * Time.deltaTime;
        orientation += rotation * Time.deltaTime;
        // we need to limit the orientation values
        // to be in the range (0 – 360)
        if (orientation < 0.0f)
            orientation += 360.0f;
        else if (orientation > 360.0f)
            orientation -= 360.0f;
        transform.Translate(displacement, Space.World);
        transform.rotation = new Quaternion();
        transform.Rotate(Vector3.up, orientation);
    }
  5. Finally, we implement the LateUpdate function, which takes care of updating the steering for the next frame according to the current frame's calculations:
    public virtual void LateUpdate ()
    {
        velocity += steering.linear * Time.deltaTime;
        rotation += steering.angular * Time.deltaTime;
        if (velocity.magnitude > maxSpeed)
        {
            velocity.Normalize();
            velocity = velocity * maxSpeed;
        }
        if (steering.angular == 0.0f)
        {
            rotation = 0.0f;
        }
        if (steering.linear.sqrMagnitude == 0.0f)
        {
            velocity = Vector3.zero;
        }
        steering = new Steering();
    }

How it works...

The idea is to be able to delegate the movement's logic inside the GetSteering() function on the behaviors that we will later build, simplifying our agent's class to a main calculation based on those.

Besides, we are guaranteed to set the agent's steering value before it is used thanks to Unity script and function execution orders.

There's more...

This is a component-based approach, which means that we have to remember to always have an Agent script attached to GameObject for the behaviors to work as expected.

See also

For further information on Unity's game loop and the execution order of functions and scripts, please refer to the official documentation available online at:

You have been reading a chapter from
Unity 5.x Game AI Programming Cookbook
Published in: Mar 2016
Publisher: Packt
ISBN-13: 9781783553570
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