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:

  1. Create a Sphere sized as (1,1,1) at (2,2,2) position, and apply the m_red Material.
  2. Create a new Prefab named Prefab-ball, and drag your Sphere into it (and then delete the Sphere from the Hierarchy ...

Get Unity 5.x Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.