Advertisement
Znimatorr

GameManager

May 27th, 2022
712
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class GameManager : MonoBehaviour
  6. {
  7.    
  8.     public float pipeFrequence = 2f;
  9.     public float pipeSpeed = 5f;
  10.     public static GameObject birb;
  11.  
  12.     [SerializeField]
  13.     private GameObject _pipePrefab;
  14.  
  15.     [SerializeField]
  16.     private GameObject _playerPrefab;
  17.  
  18.     private static List<GameObject> _pipes = new List<GameObject>();
  19.  
  20.     private static GameObject PlayerPrefab;
  21.  
  22.     float _lastTime;
  23.     void Start()
  24.     {
  25.         _lastTime = Time.time;
  26.         birb = Instantiate(_playerPrefab, new Vector3(0f, 2f, 0.125f), Quaternion.identity);
  27.         PlayerPrefab = _playerPrefab;
  28.     }
  29.  
  30.     void Update()
  31.     {
  32.         if (Time.time - _lastTime >= pipeFrequence)
  33.         {
  34.             _lastTime = Time.time;
  35.             StartCoroutine(CreatePipe());
  36.         }        
  37.     }
  38.  
  39.     public static void RestartGame()
  40.     {
  41.         foreach (var item in _pipes)
  42.         {
  43.             Destroy(item);
  44.         }
  45.  
  46.         birb = Instantiate(PlayerPrefab, new Vector3(0f, 2f, 0.125f), Quaternion.identity);
  47.     }
  48.  
  49.     private IEnumerator CreatePipe()
  50.     {
  51.         Debug.Log("Spawn Pipe");
  52.         GameObject pipe = Instantiate(_pipePrefab, new Vector3(12f, Random.Range(-3f, 3f), 0.125f), Quaternion.identity);
  53.         _pipes.Add(pipe);
  54.         pipe.GetComponent<Rigidbody2D>().velocity = Vector2.left * pipeSpeed;
  55.         yield return new WaitForSeconds(5f);
  56.         Destroy(pipe);
  57.     }
  58. }
  59.  
Advertisement
RAW Paste Data Copied
Advertisement