Choosing destinations – finding the nearest spawn point
Rather than just choosing a random spawn point or waypoint, sometimes, we want to select the one that’s closest to an object (such as the player’s GameObject). In this recipe, we will modify the previous recipe to find the nearest spawn point to the player’s cube, and then use that location to spawn a new red ball prefab:
Figure 11.10: Spawning a sphere at the spawn point capsule that is nearest the player cube
Getting ready
This recipe builds upon the previous recipe. So make a copy of that project, open it, and then follow the steps in the next section.
How to do it...
To find the nearest spawn point, follow these steps:
- Add the following method to the C# script class called
SpawnPointManager
:public GameObject GetNearestSpawnpoint (Vector3 source){ GameObject nearestSpawnPoint = spawnPoints[0]; Vector3 spawnPointPos = spawnPoints[0].transform...