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 minX; private float maxX; private float minZ; private float maxZ; void Start() { minX = -45.0f; maxX = 45.0f; minZ = -45.0f; maxZ = 45.0f; //Get Wander Position GetNextPosition...