Search Unity

Keeping track of an enemy's projectiles

Discussion in 'Scripting' started by DeathByRhino, Feb 11, 2016.

  1. DeathByRhino

    DeathByRhino

    Joined:
    Jul 30, 2015
    Posts:
    33
    So in my turn based game, my enemy shoots fireball before ending his turn or dying. When he does die, I freeze my main character to prevent them from moving. However, if he does die while a fireball is still in play, the character is frozen and thus can't dodge the fireball. I also want it to wait for all fireballs to be destroyed before it moves on to the next enemy.

    I was thinking of putting the fireballs in a list when they are created and fired, but i dont know how to check if they're still active. Any ideas?
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Sure, simply add them to a list when you create them and remove them from it when they are destroyed. Then simply looking if the list contains anything to find out if there is any still in play.
     
  3. DeathByRhino

    DeathByRhino

    Joined:
    Jul 30, 2015
    Posts:
    33
    How would I remove them once they are destroyed?
     
  4. Jaysta

    Jaysta

    Joined:
    Mar 13, 2013
    Posts:
    141
  5. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    When you spawn the missile in the enemy script and add it to your list you also tell the missile what enemy spawned it by passing it a reference. Then when the missile gets destroyed tell the owner of this.
     
  6. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Code (CSharp):
    1. Destroy(PrefabObject, 3);
    should work, you can change the 3 to how many seconds you want to wait.
     
  7. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Here's a barebone setup that would let enemies track their projectiles:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class Enemy : MonoBehaviour
    7. {
    8.     public GameObject projectilePrefab;
    9.  
    10.     private List<Projectile> projectiles = new List<Projectile>();
    11.  
    12.     // Fire a projectile and add it to the list
    13.     public void FireProjectile()
    14.     {
    15.         GameObject newProjectile = GameObject.Instantiate(projectilePrefab, transform.position, transform.rotation) as GameObject;
    16.         Projectile projectileComponent = newProjectile.GetComponent<Projectile>();
    17.         projectileComponent.SetOwner(this);    // Let projectile know of its owner, so it can notify owner when it gets destroyed.
    18.         projectiles.Add(this);    // Add to list.
    19.     }
    20.  
    21.     // Call from projectile when it is about to be destroyed.
    22.     public void ProjectileDestroyed(Projectile destroyedProjectile)
    23.     {
    24.         if (!projectiles.Contains(destroyedProjectile))
    25.         {
    26.             Debug.LogError("Projectile did not exist in list, did you forget to add it?");
    27.         }
    28.         projectiles.Remove(destroyedProjectile);    // Remove from list.
    29.         if (projectiles.Count == 0)
    30.         {
    31.             Debug.Log("No projectiles left in play.");
    32.         }
    33.     }
    34. }
    35.  
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Projectile : MonoBehaviour
    6. {
    7.     private Enemy owner;
    8.  
    9.     // Set the owner of this projectile when you spawn it.
    10.     public void SetOwner(Enemy owner)
    11.     {
    12.         this.owner = owner;
    13.     }
    14.  
    15.     // Whatever logic causes the projectile to hit something:
    16.     public void OnCollisionEnter()
    17.     {
    18.         if (!owner)
    19.         {
    20.             Debug.LogError("Owner is missing, did you forget to set it or has it been destroyed?");
    21.         }
    22.         owner.ProjectileDestroyed(this);
    23.         Destroy(gameObject);
    24.     }
    25. }
    26.  
     
    BlackDiamond012 and DeathByRhino like this.