简体   繁体   中英

Unity: Enemy Spawn / Health System

I'm working at an enemy spawn system. This is my code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class EnemyManager : MonoBehaviour
{
 public GameObject shark;                // prefab von Shark
 public GameObject instanceShark;        // globale Instanzvariable von Shark
 public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
 public float spawnTime = 3f;            // How long between each spawn.
 public int maximumSharks = 2;
 private int currentSharks;
 public int healthShark; // current Health von Shark
 public int startinghealthShark = 200;
 public float sinkSpeed = 2.5f;
 bool isDead;

 void Start ()
 {
     healthShark = startinghealthShark;
     currentSharks = 0;
 }


     void Update ()
 {
     if (currentSharks <= maximumSharks) {
         InvokeRepeating ("Spawn", spawnTime, spawnTime);
     }
     Debug.Log (currentSharks);    
 }


 void Spawn ()
 {    
     // Find a random index between zero and one less than the number of spawn points.
     int spawnPointIndex = Random.Range (0, spawnPoints.Length);

     // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
     instanceShark = Instantiate (shark, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation) as GameObject;

     currentSharks++;
     if (currentSharks >= maximumSharks) {
         CancelInvoke("Spawn");
     }
 }

 public void AddDamageToShark (int neuDamageWert) // Addiere zu damage. Public function, können andre scripts auch aufrufen
 {
     // If the enemy is dead...
     if(isDead)
         // ... no need to take damage so exit the function.
         return;

     healthShark -= neuDamageWert;

     if (healthShark <= 0) {  //tot
         Death ();
     }
     Debug.Log (healthShark);
 }

 void Death ()
 {

     // The enemy is dead.
     isDead = true;
     currentSharks--;
     Destroy (instanceShark);
     Debug.Log ("dead?");    
     return;
 }

What I want: Spawn enemies as long as maximum amount isn't reached (this part works so far) and destroy the enemy that has been shot and respawn another one (doesn't work).

This codes creates 2 sharks as enemies at the moment. The problem is when I damage one, only the last created instance gets destroyed even though I shot at the first shark. Also the health of the other instance and new spawning instance isn't affected at all.

I'd appreciate any advices, I spent ages with this code and it seems like I need some help regarding the instances - health logic.

Thank you very much

Split your spawn manager behaviour and your enemy behaviour and use interfaces to organize your code in a more SOLID approach. Turn your objects responsible for just one scope (Now your SpawnManager is current responsible for Enemy behaviours/responsabilities)

SpawnManager should be a singleton object with just one responsibility "to manage enemy spawn" with a maximunEnemyCount and a currentEnemyCount . You can spawn always when your currentEnemyCount < maximunEnemyCount .

OnTakeDamage() and OnDeath() should be interfaces of your enemy behaviour (in a separated script from SpawnManager, lets assume EnemyBehaviour script) and your destroy should target just the instance of itself Destroy(this.gameObject)

Remember to notify your SpawnManager when an enemy is dead on your OnDeath method to adjust enemyCurrentCount.

I think this is a more elegant way to do this task and less susceptible to bugs on managing enemy instances.

edited: linked singleton references that I forgot before

you'll need a unique gameObject for manager that should know how much enemies can live and how many are living, nothing more about the enemies (enemy's health is a responsibility of enemy object in this case).

each enemy need to know when he/she dies and so notify the manager to decrement its counter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM