Choosing destinations – finding a random spawn point
Many games make use of spawn points and waypoints. This recipe will show you how to choose a random spawn point, and then the instantiation of an object at that chosen point:
Figure 11.8: Sphere randomly spawned at one of the spawn point capsules
As shown in the preceding figure, the sphere has been spawned at one of the four capsule spawn points.
Getting ready
This recipe builds upon the previous recipe. So make a copy of this project, open it, and then follow the steps in the next section.
How to do it...
To find a random spawn point, follow these steps:
- In the Scene panel, create a sphere (by navigating to GameObject | 3D Object | Sphere) scaled to as
(2, 2, 2)
at position(2, 2, 2)
and apply them_red
material. - In the Project panel, create a new prefab based on your sphere by dragging the
Sphere
GameObject from the Hierarchy panel into the Project panel. Rename this new prefab...