Advertisement
SizilStank

Untitled

Nov 8th, 2022
927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CentaEnemyA1Behavior : MonoBehaviour //This is the Logical Brain for the Centa and is on the EnemyA1 and EnemyA2 GameObject
  6. {
  7.     [SerializeField] private float _enemyExplosionTime = 0.4f;
  8.  
  9.     [SerializeField] private Player _player;
  10.  
  11.     [SerializeField] private GameObject _enemyExplosion;
  12.     [SerializeField] private GameObject _enemyLaser;
  13.  
  14.     [SerializeField] private AudioManager _audioManager;
  15.  
  16.  
  17.  
  18.     private void Start()
  19.     {
  20.        
  21.         if ( !GameObject.Find("Player").TryGetComponent<Player>(out _player))
  22.         {
  23.             _player.enabled = false;
  24.             Debug.LogError("Player is NULL");
  25.         }
  26.  
  27.         if (!GameObject.Find("AudioManager").TryGetComponent<AudioManager>(out _audioManager))
  28.         {
  29.             _audioManager.enabled = false;
  30.             Debug.Log("_audioManager is NULL");
  31.         }
  32.  
  33.         InvokeRepeating("EnemyFireLaser", 2.0f, 1.5f);
  34.     }
  35.  
  36.     // Update is called once per frame
  37.     void Update()
  38.     {
  39.  
  40.         if (transform.position.y <= -9.01)
  41.         {
  42.             Vector3 randomRespawn = new Vector3(Random.Range(-9f, 9f), 10, 0);
  43.             transform.position = randomRespawn;
  44.         }
  45.     }
  46.  
  47.  
  48.     private void EnemyFireLaser()
  49.     {
  50.        // yield return new WaitForSeconds(1);
  51.         Vector3 laserPos = new Vector3(transform.position.x, transform.position.y + -0.5f, 0);
  52.         GameObject gameObject = Instantiate(_enemyLaser, laserPos, Quaternion.identity);
  53.         _audioManager.EnemyShoot();
  54.     }
  55.  
  56.     private void OnTriggerEnter2D(Collider2D other)
  57.     {
  58.  
  59.         if (other.CompareTag("Player"))
  60.         {
  61.  
  62.             GameObject explosion = Instantiate(_enemyExplosion, transform.position, Quaternion.identity);
  63.  
  64.             if (_audioManager != null)
  65.             {
  66.                 _audioManager.PlayEnemyExplosionSound();
  67.             }
  68.             else
  69.             {
  70.                 _audioManager.enabled = false;
  71.                 Debug.LogError("_audioManager is Null");
  72.             }
  73.  
  74.             Destroy(explosion, _enemyExplosionTime);
  75.             Destroy(this.gameObject);
  76.         }
  77.         else if (other.CompareTag("PlayerLaser"))
  78.         {
  79.  
  80.             Destroy(other.gameObject);
  81.  
  82.             GameObject explosion = Instantiate(_enemyExplosion, transform.position, Quaternion.identity);
  83.  
  84.             if (_player != null)
  85.             {
  86.                 _player.AddPointToScore(10);
  87.             }
  88.             else
  89.             {
  90.                 _player.enabled = false;
  91.                 Debug.LogError("_player is Null");
  92.             }
  93.  
  94.             if (_audioManager != null)
  95.             {
  96.                 _audioManager.PlayEnemyExplosionSound();
  97.             }
  98.             else
  99.             {
  100.                 _audioManager.enabled = false;
  101.                 Debug.LogError("_audioManager is Null");
  102.             }
  103.  
  104.             Destroy(explosion, _enemyExplosionTime);
  105.             Destroy(this.gameObject);
  106.         }
  107.         else if (other.CompareTag("BallsOfDeath"))
  108.         {
  109.  
  110.             GameObject explosion = Instantiate(_enemyExplosion, transform.position, Quaternion.identity);
  111.  
  112.             if (_player != null)
  113.             {
  114.                 _player.AddPointToScore(10);
  115.             }
  116.             else
  117.             {
  118.                 _player.enabled = false;
  119.                 Debug.LogError("_player is Null");
  120.             }
  121.  
  122.             if (_audioManager != null)
  123.             {
  124.                 _audioManager.PlayEnemyExplosionSound();
  125.             }
  126.             else
  127.             {
  128.                 _audioManager.enabled = false;
  129.                 Debug.LogError("_audioManager is Null");
  130.             }
  131.  
  132.             Destroy(explosion, _enemyExplosionTime);
  133.             Destroy(this.gameObject);
  134.         }
  135.     }
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement