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

Facing objects

Real-world aiming, just like in combat simulators, works a little differently to the widely-used automatic aiming process in almost every game. Imagine that you need to implement an agent controlling a tank turret or a humanized sniper; that's when this recipe comes in handy.

Getting ready

We need to make some modifications to our AgentBehaviour class:

  1. Add new member values to limit some of the existing ones:
public float maxSpeed; 
public float maxAccel; 
public float maxRotation; 
public float maxAngularAccel;

  1. Add a function called MapToRange. This function helps in finding the actual direction of rotation after two orientation values are subtracted:
public float MapToRange (float rotation) { 
    rotation %= 360.0f; 
    if (Mathf.Abs(rotation) > 180.0f) { 
        if (rotation < 0.0f) 
            rotation += 360.0f; 
        else 
            rotation -= 360.0f; 
    } 
    return rotation; 
} 
  1. Also, we need to create a basic behavior called Align that is the stepping stone for the facing algorithm. It uses the same principle as Arrive, but only in terms of rotation:
using UnityEngine; 
using System.Collections; 
 
public class Align : AgentBehaviour 
{ 
    public float targetRadius; 
    public float slowRadius; 
    public float timeToTarget = 0.1f; 
 
    public override Steering GetSteering() 
    { 
        Steering steering = new Steering(); 
        float targetOrientation = target.GetComponent<Agent>().orientation; 
        float rotation = targetOrientation - agent.orientation; 
        rotation = MapToRange(rotation); 
        float rotationSize = Mathf.Abs(rotation); 
        if (rotationSize < targetRadius) 
            return steering; 
        float targetRotation; 
        if (rotationSize > slowRadius) 
            targetRotation = agent.maxRotation; 
        else 
            targetRotation = agent.maxRotation * rotationSize / slowRadius; 
        targetRotation *= rotation / rotationSize; 
        steering.angular = targetRotation - agent.rotation; 
        steering.angular /= timeToTarget; 
        float angularAccel = Mathf.Abs(steering.angular); 
        if (angularAccel > agent.maxAngularAccel) 
        { 
            steering.angular /= angularAccel; 
            steering.angular *= agent.maxAngularAccel; 
        } 
        return steering; 
    } 
} 

How to do it...

We can now proceed to implement our facing algorithm that derives from Align:

  1. Create the Face class along with a private auxiliary target member variable:
using UnityEngine; 
using System.Collections; 
 
public class Face : Align 
{ 
    protected GameObject targetAux; 
} 
  1. Override the Awake function to set up everything and swap references:
public override void Awake() 
{ 
    base.Awake(); 
    targetAux = target; 
    target = new GameObject(); 
    target.AddComponent<Agent>(); 
} 
  1. Also, implement the OnDestroy function to handle references and avoid memory issues:
void OnDestroy () 
{ 
    Destroy(target); 
}

  1. Finally, define the GetSteering function:
public override Steering GetSteering() 
{ 
    Vector3 direction = targetAux.transform.position - transform.position; 
    if (direction.magnitude > 0.0f) 
    { 
        float targetOrientation = Mathf.Atan2(direction.x, direction.z); 
        targetOrientation *= Mathf.Rad2Deg; 
        target.GetComponent<Agent>().orientation = targetOrientation; 
    } 
    return base.GetSteering(); 
}

How it works...

The algorithm computes the internal target orientation according to the vector between the agent and the real target. Then, it just delegates the work to its parent class.

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 $19.99/month. Cancel anytime