Our next task is to make our enemy tank chase the player. We will need two scripts for this. The first will simply advertise the player's current position. The second will use that position and the NavMeshAgent component that we set up earlier to find a path to the player.
With a very short script, we can easily allow all our enemies to know the location of the player. A few short steps to create it are as follows:
Start by creating a new script in the
Scripts
folder of the Project window. Name itPlayerPosition
.This script will start with a single static variable. This variable will simply hold the current position of the player. As it is static, we will be able to easily access it from the rest of our scripts.
public static Vector3 position = Vector3.zero;