Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Check if enemies are alive?

Discussion in 'Scripting' started by ecloev, Oct 13, 2015.

  1. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    How can i check if enemies are alive?
    Thanks
    Like what is the line of code to see if a enemy with tag "Enemy" is alive?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    what do you do to determine if something is "alive" or "dead" in your game?

    What does that even mean in the context of your game?

    Not all games have "alive" things. Others represent "alive/dead" in different ways.

    For example, maybe you have a 'Health' script, and in it a float that stores the amount of health something has. If it reaches 0, it's considered "dead". Well, in that case, you'd test that float if it's less than 0.
     
  3. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Well I have a enemy spawner that spawns gameobjects all over the map.
    I want to check if there are any gameobjects with he tag Enemy that are still active and if not re spawn.
     
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    Code (CSharp):
    1. GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
    This will only return active objects with the tag.
     
  5. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Here is what I have :)
    I want it do just wait until there are no enemies still alive then repeat the process :)
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class EnemySpawner : MonoBehaviour {
    6.     public GameObject Enemy;
    7.     public float timer = 0f;
    8.     public float SpawnAreaGive = 1f;
    9.     public bool enemiesAlive = false;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.    
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.         timer += Time.deltaTime;
    19.         if (timer >= 12) {
    20.             GameObject[] enemySpawns = GameObject.FindGameObjectsWithTag("EnemySpawnPoint");
    21.             foreach(GameObject spawnPoint in enemySpawns)
    22.             {
    23.                 if(enemiesAlive == false){
    24.                     GameObject enemy;
    25.                    enemy = Instantiate(Enemy, new Vector3(spawnPoint.transform.position.x, spawnPoint.transform.position.y, spawnPoint.transform.position.z), spawnPoint.transform.rotation) as GameObject;
    26.                    enemy.name = "Enemy";
    27.                     enemiesAlive = true;
    28.                     timer = 0;
    29.                 }
    30.             }
    31.         }
    32.     }
    33. }
    34.  
     
  6. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Do I have to like say if there are no enemies alive then do that code or?
    What lines and where? :)
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    If you click the function name in the code it'll bring you to the documentation:

    http://docs.unity3d.com/ScriptReference/30_search.html?q=FindGameObjectsWithTag

    It says:

    If you've defined the idea of being "alive" as if the GameObject is 'active'. Then this function will return all 'active', or "alive", GameObjects.

    So if it's empty (length 0), no enemies exist.

    By YOUR definition.

    This is the critical part here... you're defining the concept of "aliveness". You control that definition. Use what function you have to your avail, and their definitions, to logically deduce that which you want.
     
  8. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Right now it spawns the first dose which do absolutely nothing because the actual values didn't pass over and then they stay there...
     
  9. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
  10. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Alright, the 2 things that won't hold are the 'Target' from AdvancedAI and Camera from 'Billboard'.
    Whenever I have the enemy with everything attached and I try to make a prefab with them attached it removes those 2 objects.

    Billboard:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Billboard : MonoBehaviour {
    6.     public Camera main_camera;
    7.  
    8.     // Update is called once per frame
    9.     void Update () {
    10.         transform.LookAt (transform.position + main_camera.transform.rotation * Vector3.back, main_camera.transform.rotation * Vector3.up);
    11.     }
    12. }
    13.  
    14.  
    AdvancedAI:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class AdvancedAI : MonoBehaviour
    6. {
    7.     float distance;
    8.    
    9.     public float lookAtDistance = 25.0F;
    10.     public float chaseRange = 2050.0F;
    11.     public float attackRange =4.5F ;
    12.     public float moveSpeed = 5.0F;
    13.     public float damping = 6.0F;
    14.     public float damage = 30.0F;
    15.     public float attackRepeatTime = .8F;
    16.     public Transform target;
    17.     public CharacterController controller;
    18.     private float verticalMomentum = 0f;
    19.     private float gravity = -9.8f;
    20.    
    21.     private Vector3 moveDirection = Vector3.zero;
    22.     private float attackTime;
    23.  
    24.  
    25.  
    26.     //TODO: add in a function to find controller and to locate and assign the player as the target
    27.    
    28.     void Start()
    29.     {
    30.         attackTime = Time.time;
    31.     }
    32.    
    33.     void Update()
    34.     {
    35.         distance = Vector3.Distance(target.position, transform.position);
    36.         verticalMomentum += gravity * Time.deltaTime;
    37.        
    38.         if(distance < lookAtDistance)
    39.         {
    40.             LookAt();
    41.         }
    42.        
    43.         if (distance < attackRange)
    44.         {
    45.             AttackPlayer();
    46.         }
    47.        
    48.         else if (distance < chaseRange)
    49.         {
    50.             ChasePlayer();
    51.  
    52.         }
    53.     }
    54.    
    55.     void LookAt()
    56.     {
    57.         Quaternion rotation = Quaternion.LookRotation(target.position - transform.position);
    58.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    59.     }
    60.    
    61.     void ChasePlayer()
    62.     {
    63.         moveDirection = transform.forward;
    64.         moveDirection *= moveSpeed;
    65.         moveDirection.y += verticalMomentum * Time.deltaTime;
    66.         controller.Move(moveDirection * Time.deltaTime);
    67.     }
    68.    
    69.     void AttackPlayer()
    70.     {
    71.         //TODO: Need Attack Animations
    72.         if (Time.time > attackTime)
    73.         {
    74.             target.SendMessage("damagePlayer", damage, SendMessageOptions.DontRequireReceiver);
    75.             attackTime = Time.time + attackRepeatTime;
    76.         }
    77.     }
    78.    
    79.     void ApplyDamage()
    80.     {
    81.         chaseRange += 15;
    82.         moveSpeed += 1;
    83.         lookAtDistance += 20;
    84.     }
    85. }
    86.  
     
  11. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    OK... ok... ummm:

    http://forum.unity3d.com/threads/floating-enemies.360793/
    http://forum.unity3d.com/threads/cant-hold-transform-and-camera-on-prefab.360776/
    http://forum.unity3d.com/threads/enemy-spawning.360725/
    http://forum.unity3d.com/threads/enemy-gravity-not-working.360686/
    http://forum.unity3d.com/threads/counter-issues.360710/
    http://forum.unity3d.com/threads/prefab-shooting-acting-up-upon-shot.360723/

    I see you've been very busy today.

    You're probably just diving right into this right now, not a lot of programming experience under you belt, none of that stuff.

    I suggest you go an start with some nice tutorials that walk you through these basic concepts. Instead of us holding your hand and taking a lot of time out of our own time to do so... use a tutorial that was already made to hold your hand. And then if you have any specific questions in regards to what you end up with at the end of the tutorial... come and ask us that.

    I know @BoredMormon makes a bunch of videos on youtube, check em' out:
    https://www.youtube.com/channel/UCfYhN-kdbe_WXmBAFoDOA9Q/videos
     
    BenZed likes this.
  12. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Yes... I plan to look at this.
    Only thing is that I'm literally 90% done this game.
    I have everything working besides my enemies.
    If you could just answer this one last question I'm done haha :p

    Last question:
    I have "enemies = 0", what is the correct format to say that it returned a empty array?
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class EnemySpawner : MonoBehaviour {
    6.     public GameObject Enemy;
    7.     public float timer = 0f;
    8.     public float SpawnAreaGive = 1f;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.    
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.         timer += Time.deltaTime;
    18.         if (timer >= 12) {
    19.             GameObject[] enemySpawns = GameObject.FindGameObjectsWithTag("EnemySpawnPoint");
    20.             GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
    21.             foreach(GameObject spawnPoint in enemySpawns)
    22.             {
    23.                 if(enemies = 0){
    24.                     GameObject enemy;
    25.                    enemy = Instantiate(Enemy, new Vector3(spawnPoint.transform.position.x, spawnPoint.transform.position.y, spawnPoint.transform.position.z), spawnPoint.transform.rotation) as GameObject;
    26.                    enemy.name = "Enemy";
    27.                     timer = 0;
    28.                 }
    29.             }
    30.         }
    31.     }
    32. }
    33.  
    34.  
     
  13. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    BenZed likes this.
  14. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    Code (CSharp):
    1. GameObject[] enemies = GameObject.FindGameObjectsWithTag"Enemy");
    2. if (enemies.Length == 0)
    3. {
    4.     //Do code for all enemies being inactive.
    5. }
    If you want to do it per spawn point then it will require each spawn point to keep track of its own spawned enemies and then react when they are all inactive.

    Keep in mind this code is not very performance friendly, and I would suspect from all the problems you have been having there are alot more spots of code that don't work quite right or need optimizing. I thought about a project I was working when I read that you are 90% done your game, I thought wow I was thinking the same thing about my project. The problem is, it is nowhere near done. 90% of development is bug hunting and optimizing, so to say 90% done and have the problems you are having means that you are no where near done. Just a word from my experience.
     
    AF19297024 and StarManta like this.