This article is Part 10 in a 25-Part Series.

How does a spawn look like?

Let’s start by seeing a Spawn in action(Screenshot courtesy of Youtube)

So all we need is a Star. Let’s get to it. Drag and drop a Star Sprite into the Hierarchy, rename the Game Object created as Star and reset its transform. Create an empty Game Object calling it SpawnPoint, make Star its child and reset the child's transform.

Some Coding

Add a new C# script to the SpawnPoint GameObject calling it Spawner. Declare GameObject array variable named tanks. This is to store references to the prefabs of all types of tanks. Declare a GameObject called tank. This is to store reference to the tank instantiated later. Declare 4 GameObject called smallTank, fastTank, bigTank, armoredTank with SerializedField to store the prefabs to be used by tanks array. Declare a boolean variable named isPlayer with SerializedField. This is to differentiate if the SpawnPoint is for player or enemy.

GameObject[] tanks;
GameObject tank;
[SerializeField]
bool isPlayer;
[SerializeField]
GameObject smallTank, fastTank, bigTank, armoredTank;

At the Start Monobehaviour routine of Spawner, add the below code to store the references to the prefabs of the tanks to tanks array (4 for enemy and smallTank Game Object only for the player)

void Start()
{
    if (isPlayer)
    {
        tanks = new GameObject[1] { smallTank };
    }
    else
    {
        tanks = new GameObject[4] { smallTank, fastTank, bigTank, armoredTank };
    }
}

Create a public routine called StartSpawning. Below is the full code which is to instantiate one of the tank prefabs randomly at the location of the SpawnPoint.

public void StartSpawning()
{
    tank = Instantiate(tanks[Random.Range(0,tanks.Length)], transform.position, transform.rotation);
}

Create a public routine called SpawnNewTank. Below is the full code which is to set the tank instantiated to be active. The instantiated tank is initially disabled at the start of the animation and at the end of the animation, we will run the SpawnNewTank routine to make it active.

public void SpawnNewTank()
{
    if (tank != null) tank.SetActive(true);
}

Twinkle Twinkle Little Star

Now let’s do some animation for the Star. We are using animation as a means of a timer(it takes about 1 second from the Star twinkling to spawning a tank). Go to your Project Window, create an Animation Controller and rename it as SpawnPoint. Move the Animation Controller to your preferred folder for organization. Select the SpawnPoint Game Object from the hierarchy and add an Animator Component (Miscellaneous -> Animator). From the Animator Component, set the Controller to point to the SpawnPoint Animator Controller you created earlier.

Open the Animation Window via the Unity Editor Menu(Window -> Animation). Ensure your SpawnPoint Game Object is selected in the Hierarchy, click on “Create” at the Animation Window. Unity will prompt for a name for the animation, call it StarTwinkle. Now you should be able to Add Property from the Animation Window. Add a property (Transform->Scale) from the Animation Window.

Once added, expand the drop-down for SpawnPoint:Scale, you should be able to see something like the below.

Now click on the red circle(symbolizing record). If it is depressed, means we are in recording mode. Click on the timeline at point 0:15, you should have a white line that will move to that position. Once the white line is at point 0:15, Set value of Scale.x and Scale.y to 1.5.

Do the same for the above at timeline 0.45.

Now Click on the timeline at point 0:30, that white line should move to that position together. Once the white line is at point 0:30, Set value of Scale.x and Scale.y to 1.

Do the same for the above at timeline 1.00.

Now Click on the timeline at point 1:10, that white line should move to that position together. Once the white line is at point 1:10, set the value of Scale.x and Scale.y to 0. Click on the Add Event button as indicated with the Mouse Pointer below.

A white ribbon(turns blue when selected) will appear just below the 1.10 timeline. And the inspector should come up with the title “Animation Event”. Select from the drop-down SpawnNewTank().

Now Click back to the timeline at point 0:00, that white line should move to that position together. Once the white line is at point 0:00Click on the Add Event button as indicated with the Mouse Pointer below. A white ribbon(turns blue when selected) will appear just below the 0.00 timeline. And the inspector should come up with the title “Animation Event”. Select from the drop-down StartSpawning().

Click on the red circle symbolizing record to stop recording. Now your Star Twinkle animation is completed. Click on Play of the Animation to start singing Twinkle Twinkle Little Stars along with it.

Spawning the Enemies and Player

We are almost done. Now duplicate the SpawnPoint naming the duplicate PlayerSpawnPoint. Drag and drop the PlayerTank prefab to the smallTank variable only of PlayerSpawnPoint’s inspector and check the box for isPlayer. Drag and drop the EnemyTank prefab to smallTank, fastTank, bigTank and armoredTank variable of SpawnPoint’s inspector.

Now let’s place the SpawnPoint and PlayerSpawnPoint at different locations to see the spawn happening!

Ok. Not that good a demonstration. We will need to stop the animation from running, again and again, to prevent it spamming(I mean spawning).

Select the StarTwinkle animation and uncheck the Loop Time from its inspector.

Let’s try again now!

Adding the finishing touches

Now to complete the spawning effects. Select both the SpawnPoint and PlayerSpawnPoint. Set both its Transform.Scale.x and Transform.Scale.y to 0. This is to make the Star disappear from view. The Star does not appear by default but only whenever there is a need to spawn a tank. So it is best to set it to invisible.

The next step is a means to trigger the spawning. As of now, we can only trigger it once which is by default. What we need to do is from the Animator add another state for the animation to transit to after spawning while waiting for another call to do the spawning again. Open up the Animator Window from Unity(Window -> Animator). Select the SpawnPoint animator from your Project Window.

Right Click on the Animator Window Base Layer and select Create State -> Empty.

Rename the New State as Normal then right click Set as Layer Default State.

From the Animator Window, go to the Parameters tab and create a trigger named Spawning.

From the Animator Window, right click on the Entry box and select Make Transition. Drag the arrow to the StarTwinkle box. After the boxes are joined together by the arrow, click on the arrow and add condition Spawning to it. Do the same for Normal box to StarTwinkle Box.

Add a Transition back from StarTwinkleBox to Normal.

Now let’s test the trigger for spawning.

Perfect! So that is how we can do spawning. Prefab both the SpawnPoint and PlayerSpawnPoint. All that’s left is for something to trigger the spawning. And that will be handled by the GameManager.

This article is Part 10 in a 25-Part Series.