---
title: "Lesson 2.3 - Random Animal Stampede"
content_type: tutorial
url: "https://learn.unity.com/tutorial/lesson-2-3-random-animal-stampede?language=en"
language: en
difficulty: beginner
duration_minutes: 60
unity_version:
  - "6.0"
  - "6.3"
supported_unity_versions:
  - name: "6.0"
    url: "https://learn.unity.com/tutorial/lesson-2-3-random-animal-stampede.md?language=en&version=6.0"
  - name: "6.3"
    url: "https://learn.unity.com/tutorial/lesson-2-3-random-animal-stampede.md?language=en&version=6.3"
  - name: "2022.3"
    url: "https://learn.unity.com/tutorial/lesson-2-3-random-animal-stampede.md?language=en&version=2022.3"
  - name: "2021.3"
    url: "https://learn.unity.com/tutorial/lesson-2-3-random-animal-stampede.md?language=en&version=2021.3"
available_languages:
  - name: "en"
    url: "https://learn.unity.com/tutorial/lesson-2-3-random-animal-stampede.md?language=en"
  - name: "es"
    url: "https://learn.unity.com/tutorial/leccion-2-3-estampida-de-animales-aleatorios.md?language=es"
  - name: "ja"
    url: "https://learn.unity.com/tutorial/lesson-2-3-random-animal-stampede-jp.md?language=ja"
  - name: "ko"
    url: "https://learn.unity.com/tutorial/2-3gang-mujagwi-dongmul-saengseong.md?language=ko"
last_updated: "2026-08-04T14:22:25.000Z"
---

# Lesson 2.3 - Random Animal Stampede

Learn to script a spawner that creates random animal prefabs at the top of the screen when the user presses the S key.

**Overview:**

Our animal prefabs walk across the screen and get destroyed out of bounds, but they don’t actually appear in the game unless we drag them in! In this lesson we will allow the animals to spawn on their own, in a random location at the top of the screen. In order to do so, we will create a new object and a new script to manage the entire spawning process.

**Project Outcome:**

When the user presses the S key, a randomly selected animal will spawn at a random position at the top of the screen, walking towards the player.

Overview: [Video](https://connect-mediagw.unity.com/h1/20241208/videos/178f15f2-4c33-41a1-8198-4ca22cabc0cf_CWC_U2.L3_Intro_U6.mp4) ([Transcript (en)](https://connect-mediagw.unity.com/h1/20241208/80a67b62-3467-4eeb-b587-812b63d9b1cd_CWC_U2.L3_Intro_U6.mp4.vtt))

## Materials

- [Lesson Plan 2.3 - Random Animal Stampede.pdf](https://unity-connect-prd.storage.googleapis.com/20190516/ebd9ef79-cece-4b54-be5d-8afa9f7fb147_Lesson_Plan_2.3___Random_Animal_Stampede.pdf)

## 1. Create a spawn manager

If we are going to be doing all of this complex spawning of objects, we should have a dedicated script to manage the process, as well as an object to attach it to.

[Video](https://connect-mediagw.unity.com/h1/20241208/videos/76023109-1729-4645-851f-c0709f1fe021_CWC_U2.L3.S1_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/4bc77c53-86c9-4790-b622-3368d4166f1d/versions%5B_key==%22hZGUzJc5TfSRjIpWBBEdmt%22%5D.sections%5B_key==%22JpI72pm885s7ezHBZkd9zC%22%5D.body%5B_key==%22JpI72pm885s7ezHBZkdA1c%22%5D.subtitles/CWC_U2.L3.S1_20260505_041036.srt))

1. In the Hierarchy, create an **Empty object** called “SpawnManager”
2. Create a new script called “SpawnManager”, attach it to the **Spawn Manager**, and open it
3. Declare new **_public GameObject\[ \] animalPrefabs;_**
4. In the Inspector, change the **Array size** to match your animal count, then **assign** your animals by **dragging** them from the Project window into the empty slots

   **Note:** Make sure you drag them from the **Project** window; not the Hierarchy! If you're going to spawn objects, you need to make sure you're using Prefabs, which are stored in the Project window.

## 2. Configure a temporary spawn input action

Now let’s add a Spawn Action to SpawnManager and bind it to the S key in the Inspector.

[Video](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/4bc77c53-86c9-4790-b622-3368d4166f1d/versions%5B_key==%22hZGUzJc5TfSRjIpWBBEdmt%22%5D.sections%5B_key==%22JpI72pm885s7ezHBZkdAX4%22%5D.body%5B_key==%22JpI72pm885s7ezHBZkdAZU%22%5D.overviewVideo/Unit%202.3%20-%20Step%202%20-%20Configure%20a%20temporary%20spawn%20input%20action%20-%20FINAL%20RENDER_20260603_012745.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/4bc77c53-86c9-4790-b622-3368d4166f1d/versions%5B_key==%22hZGUzJc5TfSRjIpWBBEdmt%22%5D.sections%5B_key==%22JpI72pm885s7ezHBZkdAX4%22%5D.body%5B_key==%22JpI72pm885s7ezHBZkdAZU%22%5D.subtitles/Unit%202.3%20-%20Step%202%20-%20Configure%20a%20temporary%20spawn%20input%20action%20-%20FINAL%20RENDER_20260603_012745_20260623_003956.vtt))

1. In **SpawnManager.cs**, add the **Input** **System** namespace, the action field, then enable it in the **Start** function.
2. In the **Inspector** window for your SpawnManager:
   - Find **Spawn** **Action**.
   - Select **+** **Add** **Binding**.
   - Set Path to keyboard/s (you can type to search or select **Listen** and press **S**).

![](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/4bc77c53-86c9-4790-b622-3368d4166f1d/versions%5B_key==%22hZGUzJc5TfSRjIpWBBEdmt%22%5D.sections%5B_key==%22JpI72pm885s7ezHBZkdAX4%22%5D.body%5B_key==%22JpI72pm885s7ezHBZkdAqQ%22%5D.image/Untitled%202_20260603_013238.png)

## 3. Spawn an animal if S is pressed

We’ve created an array and assigned our animals to it, but that doesn’t do much good until we have a way to spawn them during the game. Let’s create a temporary solution for choosing and spawning the animals.

[Video](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/4bc77c53-86c9-4790-b622-3368d4166f1d/versions%5B_key==%22hZGUzJc5TfSRjIpWBBEdmt%22%5D.sections%5B_key==%22f58918ad74fd%22%5D.body%5B_key==%22ec4d66cc8432%22%5D.overviewVideo/Unit%202.3%20-%20Step%203%20-%20Spawn%20an%20animal%20if%20S%20is%20pressed%20-%20FINAL%20RENDER_20260622_160029.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/4bc77c53-86c9-4790-b622-3368d4166f1d/versions%5B_key==%22hZGUzJc5TfSRjIpWBBEdmt%22%5D.sections%5B_key==%22f58918ad74fd%22%5D.body%5B_key==%22ec4d66cc8432%22%5D.subtitles/Unit%202.3%20-%20Step%203%20-%20Spawn%20an%20animal%20if%20S%20is%20pressed%20-%20FINAL%20RENDER_20260622_160029_20260623_004007.vtt))

1. In the **Update()** function, write an **if-then** statement to **instantiate** a new animal prefab at the top of the screen if the spawnAction is triggered.
2. Declare a new **public int animalIndex** and incorporate it in the **Instantiate** call, then test editing the value in the **Inspector** window.

![](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/4bc77c53-86c9-4790-b622-3368d4166f1d/versions%5B_key==%22hZGUzJc5TfSRjIpWBBEdmt%22%5D.sections%5B_key==%22f58918ad74fd%22%5D.body%5B_key==%22a2278d94bbeb%22%5D.image/Untitled%202_20260603_013433.png)

## 4. Spawn random animals from array

We can spawn animals by pressing S, but doing so only spawns an animal at the array index we specify. We need to randomize the selection so that S can spawn a random animal based on the index, without our specification.

[Video](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/4bc77c53-86c9-4790-b622-3368d4166f1d/versions%5B_key==%22hZGUzJc5TfSRjIpWBBEdmt%22%5D.sections%5B_key==%22JpI72pm885s7ezHBZkdArd%22%5D.body%5B_key==%221bd09d66217c%22%5D.overviewVideo/Unit%202.3%20-%20Step%204%20-%20Spawn%20random%20animals%20from%20array%20%28NO%20VO%29%20-%20FINAL%20RENDER_20260622_161019.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/4bc77c53-86c9-4790-b622-3368d4166f1d/versions%5B_key==%22hZGUzJc5TfSRjIpWBBEdmt%22%5D.sections%5B_key==%22JpI72pm885s7ezHBZkdArd%22%5D.body%5B_key==%221bd09d66217c%22%5D.subtitles/Unit%202.3%20-%20Step%204%20-%20Spawn%20random%20animals%20from%20array%20%28NO%20VO%29%20-%20FINAL%20RENDER_20260622_161019_20260623_004019.vtt))

1. In the if-statement checking if S is pressed, generate a random **_int animalIndex_** between 0 and the length of the array
2. Remove the global **_animalIndex_** variable, since it is only needed locally in the **if-statement**

![](https://connect-mediagw.unity.com/h1/20190522/learn/images/61490e42-d835-4f1d-bff8-933f2e59d47a_2_3_3.png)

## 5. Randomize the spawn location

We can press S to spawn random animals from animalIndex, but they all pop up in the same place! We need to randomize their spawn position, so they don’t march down the screen in a straight line.

[Video](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/4bc77c53-86c9-4790-b622-3368d4166f1d/versions%5B_key==%22hZGUzJc5TfSRjIpWBBEdmt%22%5D.sections%5B_key==%22JpI72pm885s7ezHBZkdB7M%22%5D.body%5B_key==%22JpI72pm885s7ezHBZkdB9m%22%5D.overviewVideo/Unit%202.3%20-%20Step%205%20-%20Randomize%20the%20spawn%20location%20%28NO%20VO%29%20-%20FINAL%20RENDER_20260622_035555.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/4bc77c53-86c9-4790-b622-3368d4166f1d/versions%5B_key==%22hZGUzJc5TfSRjIpWBBEdmt%22%5D.sections%5B_key==%22JpI72pm885s7ezHBZkdB7M%22%5D.body%5B_key==%22JpI72pm885s7ezHBZkdB9m%22%5D.subtitles/Unit%202.3%20-%20Step%205%20-%20Randomize%20the%20spawn%20location%20%28NO%20VO%29%20-%20FINAL%20RENDER_20260622_035555_20260623_004036.vtt))

1. **Replace** the X value for the Vector3 with **_Random.Range(-20, 20)_**, then test
2. Within the **if-statement**, make a new local **_Vector3 spawnPos_** variable
3. At the top of the class, create **_private float_** variables for **_spawnRangeX_** and **_spawnPosZ_**

![](https://connect-mediagw.unity.com/h1/20190529/learn/images/dace9f7f-f781-4ab8-ae03-0a49813440f6_2.3_4.PNG)

## 6. Change the perspective of the camera

Our Spawn Manager is coming along nicely, so let’s take a break and mess with the camera.Changing the camera’s perspective might offer a more appropriate view for this top-down game.

[Video](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/4bc77c53-86c9-4790-b622-3368d4166f1d/versions%5B_key==%22hZGUzJc5TfSRjIpWBBEdmt%22%5D.sections%5B_key==%22JpI72pm885s7ezHBZkdBVY%22%5D.body%5B_key==%22JpI72pm885s7ezHBZkdBXy%22%5D.overviewVideo/Unit%202.3%20-%20Step%206%20-%20Change%20the%20perspective%20of%20the%20camera%20-%20FINAL%20NEW_20260625_202835.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/4bc77c53-86c9-4790-b622-3368d4166f1d/versions%5B_key==%22hZGUzJc5TfSRjIpWBBEdmt%22%5D.sections%5B_key==%22JpI72pm885s7ezHBZkdBVY%22%5D.body%5B_key==%22JpI72pm885s7ezHBZkdBXy%22%5D.subtitles/Unit%202.3%20-%20Step%206%20-%20Change%20the%20perspective%20of%20the%20camera%20-%20FINAL%20NEW_20260625_202926.vtt))

1. Toggle between **Perspective** and **Isometric** view in Scene view to appreciate the difference
2. Select the **camera** and change the **Projection** from “Perspective” to “Orthographic”

## 7. Lesson Recap

## New Functionality

- The player can press the S to spawn an animal
- Animal selection and spawn location are randomized
- Camera projection (perspective/orthographic) selected

## New Concepts & Skills

- Spawn Manager
- Arrays
- Keycodes
- Random generation
- Local vs Global variables
- Perspective vs Isometric projections

## Next Lesson

- Using collisions to feed our animals!

## Check your scripts

SpawnManager.cs

```csharp
using UnityEngine;
using UnityEngine.InputSystem;

public class SpawnManager : MonoBehaviour
{
    public GameObject[] animalPrefabs;
    public InputAction spawnAction;

    private float spawnRangeX = 20;
    private float spawnPosZ = 20;

    void Start()
    {
        spawnAction.Enable();
    }

    void Update()
    {
        if (spawnAction.triggered)
        {
            // Randomly generate animal index and spawn position
            Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX),
            0, spawnPosZ);
            int animalIndex = Random.Range(0, animalPrefabs.Length);
            Instantiate(animalPrefabs[animalIndex], spawnPos,
            animalPrefabs[animalIndex].transform.rotation);
        }
    }
}
```
