Advertisement
Placido_GDD

ObstacleManager

Feb 22nd, 2022
1,088
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ObstacleManager : MonoBehaviour
  6. {
  7.     // Start is called before the first frame update
  8.     public GameObject[] obstaclePrefabs;
  9.     private GameObject tempHolder;
  10.     public Vector3[] obstacleSpawnPoints;
  11.     public float obstacleSpawnRate;
  12.     public float obstacleDur;
  13.     public float obstacleSpeed;
  14.     private int obstacleID;
  15.     private int spawnPointID;
  16.     public List<GameObject> spawnedObstacles = new List<GameObject>();
  17.     [HideInInspector] public List<GameObject> removeObstacles = new List<GameObject>();
  18.     [HideInInspector] public ObstacleData OD;
  19.     void Start()
  20.     {
  21.         InvokeRepeating("SpawnObstacles", 0.0f, obstacleSpawnRate);
  22.     }
  23.  
  24.     // Update is called once per frame
  25.     void Update()
  26.     {
  27.         MoveObstacles();
  28.     }
  29.  
  30.     void MoveObstacles()
  31.     {
  32.         for (int i = 0; i < spawnedObstacles.Count; i++)
  33.         {
  34.             if (spawnedObstacles[i] != null)
  35.             {
  36.                 spawnedObstacles[i].transform.position += (Vector3.back * obstacleSpeed) * Time.deltaTime;
  37.             }
  38.         }
  39.     }
  40.     void SpawnObstacles()
  41.     {
  42.         obstacleID = Random.Range(0,obstaclePrefabs.Length);
  43.         spawnPointID = Random.Range(0,obstacleSpawnPoints.Length);
  44.         tempHolder = Instantiate(obstaclePrefabs[obstacleID],obstacleSpawnPoints[spawnPointID], Quaternion.identity);
  45.         spawnedObstacles.Add(tempHolder);
  46.         OD = tempHolder.GetComponent<ObstacleData>();
  47.         OD.DestroyTimer = obstacleDur;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement