How to do it...

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

  1. Create the class for the basic rival agent, as shown in the following code:
using UnityEngine; 
 
public class RacingRival : MonoBehaviour 
{ 
    public float distanceThreshold; 
    public float maxSpeed; 
    public Vector3 randomPos; 
    protected Vector3 targetPosition; 
    protected float currentSpeed; 
    protected RacingCenter ghost; 
} 
  1. Implement the Start function, as follows:
void Start() 
{ 
    ghost = FindObjectOfType<RacingCenter>(); 
} 
  1. Define the Update function for handling the target position to follow, as shown in the following code:
public virtual void Update() { targetPosition = transform.position + randomPos; AdjustSpeed(targetPosition); ...

Get Unity 2018 Artificial Intelligence Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.