Advertisement
GoodNoodle

Cannon

Aug 3rd, 2023
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Cannon : MonoBehaviour
  6. {
  7.     public GameObject lavaRock;
  8.  
  9.     [SerializeField] public bool IsInstanciated;
  10.  
  11.     public PlayerController player;
  12.  
  13.     public int ballAmout = 3;
  14.  
  15.     public bool canShoot;
  16.  
  17.     public GameManager gameManager;
  18.  
  19.  
  20.     private void Start()
  21.     {
  22.         canShoot = true;
  23.         if (player != null)
  24.         {
  25.             player = FindObjectOfType<PlayerController>();
  26.         }
  27.  
  28.     }
  29.     private void Update()
  30.     {
  31.         if (!lavaRock.activeInHierarchy)
  32.         {
  33.             if (player != null)
  34.             {
  35.                 StartCoroutine(SpawnLavaRock());
  36.             }
  37.         }
  38.     }
  39.  
  40.     IEnumerator SpawnLavaRock()
  41.     {
  42.         if (canShoot)
  43.         {
  44.             if (Vector2.Distance(player.transform.position, this.transform.position) < 5)
  45.             {
  46.  
  47.                 if (!IsInstanciated)
  48.                 {
  49.                     IsInstanciated = true;
  50.                     ballAmout--;
  51.                     Instantiate(lavaRock, transform.position, Quaternion.identity);
  52.                     yield return new WaitForSeconds(1f);
  53.                     IsInstanciated = false;
  54.                 }
  55.             }
  56.         }
  57.  
  58.         if(ballAmout <= 0)
  59.         {
  60.             canShoot= false;
  61.             StopCoroutine(SpawnLavaRock());
  62.             gameManager.EndGame();
  63.         }
  64.        
  65.     }
  66.    
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement