Advertisement
LeeMace

Spawn manager from Top/Left/Right of Screen

Nov 29th, 2022 (edited)
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SpawnManager : MonoBehaviour
  6. {
  7.     //array for the prefab animals
  8.     public GameObject[] animalPrefabs;
  9.  
  10.     private float spawnRangeX = 20;
  11.     private float spawnPosZ = 30;
  12.  
  13.     public float sideSpawnMinZ;
  14.     public float sideSpawnMaxZ;
  15.     public float sideSpawnX;
  16.  
  17.     private float spawnDelay = 2;
  18.     private float spawnInterval = 5f;
  19.     void Start()
  20.     {
  21.         InvokeRepeating("SpawnRandomAnimal", spawnDelay, spawnInterval);
  22.         InvokeRepeating("SpawnLeftAnimal", spawnDelay, spawnInterval);
  23.         InvokeRepeating("SpawnRightAnimal", spawnDelay, spawnInterval);
  24.  
  25.     }
  26.  
  27.     void Update()
  28.     {
  29.         //pressing S spawns an animal
  30.        // if (Input.GetKeyDown(KeyCode.S))
  31.         {
  32.       //     SpawnRandomAnimal();
  33.         }
  34.     }
  35.     void SpawnRandomAnimal()
  36.     {
  37.         //randomises the position of spawn on X axis
  38.         Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 0, spawnPosZ);
  39.         //randomises the animal spawned
  40.         int animalIndex = Random.Range(0, animalPrefabs.Length);
  41.         //spawns animal at position
  42.         Instantiate(animalPrefabs[animalIndex], spawnPos, animalPrefabs[animalIndex].transform.rotation);
  43.     }
  44.  
  45.     void SpawnLeftAnimal ()
  46.     {
  47.     int animalIndex = Random.Range(0, animalPrefabs.Length);
  48.         Vector3 spawnPos = new Vector3 (-sideSpawnX, 0, Random.Range(sideSpawnMinZ, sideSpawnMaxZ));
  49.         Vector3 rotation = new Vector3 (0, 90, 0);
  50.         Instantiate(animalPrefabs[animalIndex], spawnPos, Quaternion.Euler(rotation));
  51.     }
  52.  
  53.     void SpawnRightAnimal()
  54.     {
  55.         int animalIndex = Random.Range(0, animalPrefabs.Length);
  56.         Vector3 spawnPos = new Vector3(sideSpawnX, 0, Random.Range(sideSpawnMinZ, sideSpawnMaxZ));
  57.         Vector3 rotation = new Vector3(0, -90, 0);
  58.  
  59.         Instantiate(animalPrefabs[animalIndex], spawnPos, Quaternion.Euler(rotation));
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement