This is the fifth tutorial in a series about Object Management. It's about making objects spawn in more varied patterns, configurable per level.

This tutorial is made with Unity 2017.4.4f1.

Spawn Points

Our simple game is about spawning random shapes. The material and color of each shape are randomly chosen, and so are its position, rotation, and scale. Although the spawn points are randomized, they are constrained to a spherical region with a radius of five units, centered on the world origin. Once enough objects have been spawned, they will form a recognizable sphere. This is the spawn zone that we have hard-coded into our game.

We do not have to restrict ourselves to a single spawn zone. We could make it possible for shapes to spawn in different configurations. To do so, we have to replace our fixed code with a configurable spawn zone.

Spawn Zone Component Create a new SpawnZone component type. Its only purpose is to provide spawn points, so give it a Vector3 SpawnPoint property. This provides a way to get points—not to set them—so it only needs a get block. That makes it a getter-only or readonly property. We'll begin by still returning a random point inside a sphere with a radius of five units. using UnityEngine; public class SpawnZone : MonoBehaviour { public Vector3 SpawnPoint { get { return Random.insideUnitSphere * 5f; } } } Add a Spawn Zone game object to the main scene and attach the new component to it. We now have a spawn zone in the game, but nothing's using it yet. Spawn zone object.

Using the Zone The next step is to have Game retrieve its spawn points from the separate spawn zone. Add a public field for that and use it in CreateShape to get a spawn point. public SpawnZone spawnZone; … void CreateShape () { Shape instance = shapeFactory.GetRandom(); Transform t = instance.transform; //t.localPosition = Random.insideUnitSphere * 5f; t.localPosition = spawnZone.SpawnPoint; … } Connect the spawn zone via the inspector. Although the game still behaves the same, it now relies on the Spawn Zone object. Spawn zone reference.

Transforming the Zone Because the spawn zone is part of a game object, we can move it around. To have that influence the spawn points, add the object's position to the random point. By using the position property of the Transform component instead of localPosition , it's possible to make the spawn zone a child of another object. That way the spawn zone could be attached to something else, which might be moving. public Vector3 SpawnPoint { get { return Random.insideUnitSphere * 5f + transform.position ; } } We can go a step further and apply the entire transformation of the game object hierarchy to the spawn point. Then we can also rotate and scale the zone. To do this, invoke the TransformPoint method of the zone's Transform component, with the random point as an argument. We can now do away with the multiplication by five, instead controlling the zone's radius by setting its object's scale. //return Random.insideUnitSphere * 5f + transform.position; return transform.TransformPoint(Random.insideUnitSphere); Spawn zone scaled to five units. This also makes it possible to deform the sphere, by using a nonuniform scale. Spawn zone with 45° Z rotation and scale (10, 2, 5).

Surface Only We don't have to pick a spawn point inside the sphere's radius. It is also possible to get a point on the sphere's surface, by using Random.onUnitSphere instead of Random.insideUnitSphere . Let's make that an option, by adding a surfaceOnly toggle field to the zone. [SerializeField] bool surfaceOnly; public Vector3 SpawnPoint { get { return transform.TransformPoint( surfaceOnly ? Random.onUnitSphere : Random.insideUnitSphere ); } } Spawning only on the zone's surface. Spawning only on the surface makes the shape of the sphere more obvious. Surface-only vs. volume.