Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class SpawnManager : MonoBehaviour
- {
- //array for the prefab animals
- public GameObject[] animalPrefabs;
- private float spawnRangeX = 20;
- private float spawnPosZ = 30;
- public float sideSpawnMinZ;
- public float sideSpawnMaxZ;
- public float sideSpawnX;
- private float spawnDelay = 2;
- private float spawnInterval = 5f;
- void Start()
- {
- InvokeRepeating("SpawnRandomAnimal", spawnDelay, spawnInterval);
- InvokeRepeating("SpawnLeftAnimal", spawnDelay, spawnInterval);
- InvokeRepeating("SpawnRightAnimal", spawnDelay, spawnInterval);
- }
- void Update()
- {
- //pressing S spawns an animal
- // if (Input.GetKeyDown(KeyCode.S))
- {
- // SpawnRandomAnimal();
- }
- }
- void SpawnRandomAnimal()
- {
- //randomises the position of spawn on X axis
- Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 0, spawnPosZ);
- //randomises the animal spawned
- int animalIndex = Random.Range(0, animalPrefabs.Length);
- //spawns animal at position
- Instantiate(animalPrefabs[animalIndex], spawnPos, animalPrefabs[animalIndex].transform.rotation);
- }
- void SpawnLeftAnimal ()
- {
- int animalIndex = Random.Range(0, animalPrefabs.Length);
- Vector3 spawnPos = new Vector3 (-sideSpawnX, 0, Random.Range(sideSpawnMinZ, sideSpawnMaxZ));
- Vector3 rotation = new Vector3 (0, 90, 0);
- Instantiate(animalPrefabs[animalIndex], spawnPos, Quaternion.Euler(rotation));
- }
- void SpawnRightAnimal()
- {
- int animalIndex = Random.Range(0, animalPrefabs.Length);
- Vector3 spawnPos = new Vector3(sideSpawnX, 0, Random.Range(sideSpawnMinZ, sideSpawnMaxZ));
- Vector3 rotation = new Vector3(0, -90, 0);
- Instantiate(animalPrefabs[animalIndex], spawnPos, Quaternion.Euler(rotation));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement