Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Enemy fire starts after enemy explosion

Discussion in 'Getting Started' started by CristianPalmas, Mar 8, 2021.

  1. CristianPalmas

    CristianPalmas

    Joined:
    Jul 21, 2019
    Posts:
    2
    Hi, I'm new to Unity and I'm attending a Udemy course on creating a galaxy shooter 2D game with C#. The game is complete and bug free, except for a spiting behaviour. I want he enemy fire be destroyed after the object destruction of the enemy. But unfortunately the enemy dies and it happens often that its fire spawns in the void space at the same position it explodes.
    Here's the code in Update():

    Code (CSharp):
    1. void Update()
    2.     {
    3.         CalculateMovement(); // It doesn't matter in my case
    4.  
    5.         if (Time.time > _canFire)
    6.         {
    7.            
    8.             _fireRate = Random.Range(3.0f, 7.0f);
    9.             _canFire = Time.time + _fireRate;
    10.             _enemyFire = Instantiate(_laserPrefab, transform.position, Quaternion.identity);
    11.         }
    12.     }
    Here is the OnTriggerEnter2D excerpt:

    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         if (other.CompareTag("Player"))
    4.         {
    5.             Player player = other.GetComponent<Player>();
    6.  
    7.             if (player != null)
    8.             {
    9.                 player.Damage();
    10.             }
    11.  
    12.            // Here I tried Destroy(_enemyFire); but it did not solve the problem
    13.             _anim.SetTrigger("OnEnemyDeath");
    14.             _speed = 0;          
    15.             Destroy(GetComponent<Collider2D>());
    16.             _audioSource.Play();          
    17.             Destroy(this.gameObject, 2.8f);
    18.         }
    19.  
    20.         if (other.CompareTag("Laser"))
    21.         {
    22.             Destroy(other.gameObject);
    23.  
    24.             if (_gameManager.isCoOpMode == false)
    25.             {
    26.                 if (_playerOne != null)
    27.                 {
    28.                     _playerOne.AddScore();
    29.                 }
    30.             }
    31.             else
    32.             {
    33.                 if (_playerOne != null)
    34.                 {
    35.                     _playerOne.AddScore();
    36.                 }
    37.  
    38.                 if (_playerTwo != null)
    39.                 {
    40.                     _playerTwo.AddScore();
    41.                 }
    42.             }
    43.  
    44.            // Also here I put Destroy(_enemyFire); but it did not solve the problem
    45.             _anim.SetTrigger("OnEnemyDeath");
    46.             _speed = 0;
    47.             Destroy(GetComponent<Collider2D>());
    48.             _audioSource.Play();
    49.             Destroy(this.gameObject, 2.8f);
    50.         }
    51.     }
    I think the problem is that the fire instantiates just before explosion and, after a random time, it appears. I cannot understand how to avoid that. Consider that I destroy the enemy object after 2.8 seconds because an animation of about 2.8 seconds is attached to it to render the explosion.
    Thanks in advance for the help.
     
  2. CristianPalmas

    CristianPalmas

    Joined:
    Jul 21, 2019
    Posts:
    2
    Well, in the meantime I was waiting for the thread to be approved, I found a possible solution, focusing my attention just on the instantiation time, instead on the lifecycle of the prefab. Since _canfire establishes the time of the _enemyFire spawing, I added this lines fo code:

    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         if (other.CompareTag("Player"))
    4.         {
    5.             Player player = other.GetComponent<Player>();
    6.             _canFire = 1000; // Added right now
    7.  
    8.           ...
    9.  
    10.         if (other.CompareTag("Laser"))
    11.         {
    12.             Destroy(other.gameObject);
    13.             _canFire = 1000; // Added right now
    14.  
    15.           ...            
    16.             _anim.SetTrigger("OnEnemyDeath");
    17.             _speed = 0;
    18.             Destroy(GetComponent<Collider2D>());
    19.             _audioSource.Play();
    20.             Destroy(this.gameObject, 2.8f);
    21.         }
    22.     }
    With this trick I stopped the _enemyFire spawning for the time necessary to destroy enemy objects and all the objects related. I think it would have been enough setting to 10 seconds.

    Since we are here, any other (and more elegant) solution?