Creating an AI character

Our NPC will be roaming around the scene in a random direction. It'll have the following two senses:

  • The perspective sense will check whether the tank aspect is within a set visible range and distance
  • The touch sense will detect if the enemy aspect has collided with its box collider, which we'll be adding to the tank in a later step

Because our player tank will have the PLAYER aspect type, the NPC will be looking for any aspectType not equal to its own.

The code in the Wander.cs file is as follows:

using UnityEngine;public class Wander : MonoBehaviour {    private Vector3 targetPosition;    private float movementSpeed = 5.0f;    private float rotationSpeed = 2.0f;    private float targetPositionTolerance = 3.0f; private float ...

Get Unity 2017 Game AI Programming - Third 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.