Reader small image

You're reading from  Unity 5.x Game AI Programming Cookbook

Product typeBook
Published inMar 2016
PublisherPackt
ISBN-139781783553570
Edition1st Edition
Tools
Right arrow
Author (1)
Jorge Palacios
Jorge Palacios
author image
Jorge Palacios

Jorge Palacios is a software and game developer with a BS in computer science and eight years of professional experience. He's been developing games for the last five years in different roles, from tool developer to lead programmer. Mainly focused on artificial intelligence and gameplay programming, he is currently working with Unity and HTML5. He's also a game-programming instructor, speaker, and game-jam organizer.
Read more about Jorge Palacios

Right arrow

Chapter 8. Miscellaneous

In this chapter, you will learn different techniques for:

  • Handling random numbers better

  • Building an air-hockey rival

  • Devising a table-football competitor

  • Creating a tennis rival

  • Creating mazes procedurally

  • Implementing a self-driving car

  • Managing race difficulty using a rubber-banding system

Introduction


In this final chapter, we will introduce new techniques, and use algorithms that we have learned in the previous chapters in order to create new behaviors that don't quite fix in a definite category. This is a chapter to have fun and get another glimpse of how to mix different techniques in order to achieve different goals.

Handling random numbers better


Sometimes, we need to create random behaviors that don't differ too much from a pivot point; this is the case of an aiming behavior. A normalized random behavior will shoot equally along the x and the y axes over a given distance from the aiming point. However, we would like most of the bullets to aim closer to the target because that's the expected behavior.

Most of the random functions out there return normalized values along the range given to them, and those are the expected results. Nonetheless, this is not completely useful for certain features in game development, as we just said. We will implement a random function to be used in our games with normal distribution instead of a normal distribution.

Getting ready

It is important to understand the differences between uniform and normal distribution. In the following figure, we can see a graphical representation of the behavior we're looking for by applying normal distribution with the example mentioned in...

Building an air-hockey rival


Air hockey is probably one of the most popular games enjoyed by players of all ages during the golden age of arcades, and they are still found everywhere. With the advent of touchscreen mobile devices, developing an air-hockey game is a fun way to not only test physics engines, but also to develop intelligent rivals despite the apparently low complexity of the game.

Getting ready

This is a technique based on some of the algorithms that we learned in Chapter 1, Movement, such as Seek, Arrive, and Leave, and the ray casting knowledge that is employed in several other recipes, such as path smoothing.

It is necessary for the paddle game object to be used by the agent to have the AgentBehaviour, Seek, and Leave components attached, as it is used by the current algorithm. Also, it is important to tag the objects used as walls, that is, the ones containing the box colliders, as seen in the following figure:

Finally, it is important to create an enum type for handling the...

Devising a table-football competitor


Another common table game that has made its way into the digital realm is table football. In this recipe, we will create a competitor, imitating the way a human plays the game and using some techniques that emulate human senses and limitations.

Getting ready

In this recipe, we will use the knowledge gained from Chapter 5, Agent Awareness, and the emulation of vision.

First, it is important to have a couple of enum data structures, as shown in the following code:

public enum TFRAxisCompare
{
    X, Y, Z
}

public enum TFRState
{
    ATTACK, DEFEND, OPEN
}

How to do it…

This is a very extensive recipe. We'll build a couple of classes, one for the table-football bar and the other for the main AI agent that handles the bars, as follows:

  1. Create a class for the bar that will be handled by the AI:

    using UnityEngine;
    using System.Collections;
    
    public class TFRBar : MonoBehaviour
    {
        [HideInInspector]
        public int barId;
        public float barSpeed;
        public float...

Creating mazes procedurally


This is a completely new recipe oriented toward having fun while creating maps and levels procedurally. The main recipe works by creating a maze completely procedurally. Furthermore, we will explore a gray area, where both level design and procedurally generated content meet.

Getting ready

In this recipe, it is important to understand the concepts of Binary Space Partitioning and the Breadth-first Search algorithm learned in Chapter 2, Navigation.

How to do it…

We will implement two classes, one for the nodes to be partitioned and one for holding all the nodes and the maze representation, as follows:

  1. Create the BSPNode class and its members:

    using UnityEngine;
    
    [System.Serializable]
    public class BSPNode
    {
        public Rect rect;
        public BSPNode nodeA;
        public BSPNode nodeB;
    }
  2. Implement the class constructor:

    public BSPNode(Rect rect)
    {
        this.rect = rect;
        nodeA = null;
        nodeB = null;
    }
  3. Define the function for splitting the node into two subregions:

    public void...

Implementing a self-driving car


What fun is a racing game without competitors? This is one of the most difficult subjects in artificial intelligence for games. It is usually tackled by creating cheater agents that disable certain limitations that are always imposed on the player, such as physics behaviors; this is because these limitations can create erratic or imprecise behaviors when evaluated by AI. In our case, we will approach the problem organically using techniques from a previous chapter.

Getting ready

In this chapter, we will explore how to create an autonomous car using advanced techniques from Chapter 1, Movement, such as following a path and avoiding walls. So, it is important to have grasped the knowledge behind them.

How to do it...

  1. Create an empty GameObject.

  2. Attach the Agent component.

  3. Attach the FollowPath component.

  4. Attach the WallAvoid component.

  5. Create the track using the track pieces with the PathNode component.

  6. Tag the track borders as walls.

  7. Make sure the track is complete.

How...

Managing race difficulty using a rubber-banding system


We usually want to create experiences that adapt to the player, and racing games are a good field for this, given that there is this gap of the cheater agent.

In this case, we will explore a middle ground for this using a framework that allows you to come up with your own heuristic for managing the speed of the vehicle given its status. It doesn't matter if it is an arcade racing game or simulation; the framework aims to work in a similar fashion for both the cases.

Getting ready

It is important to have grasped the basic skills in Chapter 1, Movement, in order to be able to develop a strategy to extend the framework for your own needs—that is, understanding the principles of how the agent class works and how the behaviors help the player move toward an object. In a nutshell, we are talking about vector operations.

How to do it...

We will implement three different classes for handling low-level and high-level AIs as follows:

  1. Create the class...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity 5.x Game AI Programming Cookbook
Published in: Mar 2016Publisher: PacktISBN-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.
undefined
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 €14.99/month. Cancel anytime

Author (1)

author image
Jorge Palacios

Jorge Palacios is a software and game developer with a BS in computer science and eight years of professional experience. He's been developing games for the last five years in different roles, from tool developer to lead programmer. Mainly focused on artificial intelligence and gameplay programming, he is currently working with Unity and HTML5. He's also a game-programming instructor, speaker, and game-jam organizer.
Read more about Jorge Palacios