Book Image

Unity 2018 Artificial Intelligence Cookbook - Second Edition

By : Jorge Palacios
Book Image

Unity 2018 Artificial Intelligence Cookbook - Second Edition

By: Jorge Palacios

Overview of this book

Interactive and engaging games come with intelligent enemies, and this intellectual behavior is combined with a variety of techniques collectively referred to as Artificial Intelligence. Exploring Unity's API, or its built-in features, allows limitless possibilities when it comes to creating your game's worlds and characters. This cookbook covers both essential and niche techniques to help you take your AI programming to the next level. To start with, you’ll quickly run through the essential building blocks of working with an agent, programming movement, and navigation in a game environment, followed by improving your agent's decision-making and coordination mechanisms – all through hands-on examples using easily customizable techniques. You’ll then discover how to emulate the vision and hearing capabilities of your agent for natural and humanlike AI behavior, and later improve the agents with the help of graphs. This book also covers the new navigational mesh with improved AI and pathfinding tools introduced in the Unity 2018 update. You’ll empower your AI with decision-making functions by programming simple board games, such as tic-tac-toe and checkers, and orchestrate agent coordination to get your AIs working together as one. By the end of this book, you’ll have gained expertise in AI programming and developed creative and interactive games.
Table of Contents (12 chapters)

Creating a jump system

Imagine that we're developing a cool action game where the player is capable of escaping using cliffs and rooftops. In that case, the enemies need to be able to chase the player and be smart enough to discern whether to take the jump and gauge how to do it.

Getting ready

We need to create a basic matching-velocity algorithm and the notion of jump pads and landing pads in order to emulate velocity math so that we can reach them.

The following is the code for the VelocityMatch behavior:

using UnityEngine; 
using System.Collections; 
 
public class VelocityMatch : AgentBehaviour { 
     
    public float timeToTarget = 0.1f; 
 
    public override Steering GetSteering() 
    { 
        Steering steering = new Steering(); 
        steering.linear = target.GetComponent<Agent>().velocity - agent.velocity; 
        steering.linear /= timeToTarget; 
        if (steering.linear.magnitude > agent.maxAccel) 
            steering.linear = steering.linear.normalized * agent.maxAccel; 
 
        steering.angular = 0.0f; 
        return steering; 
    } 
} 

Also, it's important to create a data type called JumpPoint:

using UnityEngine; 
using System.Collections; 
 
public class JumpPoint  
{ 
    public Vector3 jumpLocation; 
    public Vector3 landingLocation; 
 
    //The change in position from jump to landing 
    public Vector3 deltaPosition; 
 
    public JumpPoint () : this (Vector3.zero, Vector3.zero) 
    { 
    } 
 
    public JumpPoint(Vector3 a, Vector3 b) 
    { 
        this.jumpLocation = a; 
        this.landingLocation = b; 
        this.deltaPosition = this.landingLocation - this.jumpLocation; 
    } 
} 

How to do it...

  1. Create the Jump script along with its member variables:
using UnityEngine; 
using System.Collections; 
 
public class Jump : VelocityMatch 
{ 
    public JumpPoint jumpPoint; 
    public float maxYVelocity; 
    public Vector3 gravity = new Vector3(0, -9.8f, 0); 
    bool canAchieve = false; 
}

  1. Implement the SetJumpPoint function:
public void SetJumpPoint(Transform jumpPad, Transform landingPad) 
{ 
    jumpPoint = new JumpPoint(jumpPad.position, landingPad.position); 
} 
  1. Add a function to calculate the target:
protected void CalculateTarget() 
{ 
    target = new GameObject(); 
    target.AddComponent<Agent>(); 
    target.transform.position = jumpPoint.jumpLocation; 
    //Calculate the first jump time 
    float sqrtTerm = Mathf.Sqrt(2f * gravity.y * jumpPoint.deltaPosition.y + maxYVelocity * agent.maxSpeed); 
    float time = (maxYVelocity - sqrtTerm) / gravity.y; 
    //Check if we can use it, otherwise try the other time 
    if (!CheckJumpTime(time)) 
    { 
        time = (maxYVelocity + sqrtTerm) / gravity.y; 
    } 
} 
  1. Implement the CheckJumpTime function, to decide whether it's worth taking the jump:
private bool CheckJumpTime(float time) 
{ 
    //Calculate the planar speed 
    float vx = jumpPoint.deltaPosition.x / time; 
    float vz = jumpPoint.deltaPosition.z / time; 
    float speedSq = vx * vx + vz * vz; 
    //Check it to see if we have a valid solution 
    if (speedSq < agent.maxSpeed * agent.maxSpeed) 
    { 
        target.GetComponent<Agent>().velocity = new Vector3(vx, 0f, vz); 
        canAchieve = true; 
        return true; 
    } 
    return false; 
}

  1. Finally, define the GetSteering function:
public override Steering GetSteering() 
{ 
    Steering steering = new Steering(); 
    if (target == null) 
    { 
        CalculateTarget(); 
    } 
    if (!canAchieve) 
    { 
        return steering; 
    } 
    //Check if we've hit the jump point 
    if (Mathf.Approximately((transform.position - target.transform.position).magnitude, 0f) && 
        Mathf.Approximately((agent.velocity - target.GetComponent<Agent>().velocity).magnitude, 0f)) 
    { 
        // call a jump method based on the Projectile behaviour 
        return steering; 
    } 
    return base.GetSteering(); 
} 

How it works...

The algorithm takes into account the agent's velocity and calculates whether it can reach the landing pad or not. If it judges that the agent can, it tries to match the vertical velocity while seeking the landing pad's position.