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; ...

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.