Choosing destinations – find the nearest (or a random) spawn point
Many games make use of spawn points and waypoints. This recipe demonstrates two very common examples of spawning—the choosing of either a random spawn point, or the nearest one to an object of interest (such as the Player's character), and then the instantiation of an object at that chosen point.
Getting ready
This recipe builds upon the previous recipe. So, make a copy of this project, open it, and then follow the next steps.
How to do it...
To find a random spawn point, follow these steps:
Create a Sphere sized as (1,1,1) at (2,2,2) position, and apply the
m_red
Material.Create a new Prefab named
Prefab-ball
, and drag your Sphere into it (and then delete the Sphere from the Hierarchy panel).Create a new capsule object named
Capsule-spawnPoint
at (3, 0.5, 3), give it the tag asRespawn
(this is one of the default tags that Unity provides).Note
For testing, we'll leave these Respawn points visible. For the final game, we'll then...