繁体   English   中英

团结:敌人产生/健康系统

[英]Unity: Enemy Spawn / Health System

我在一个敌方生成系统上工作。 这是我的代码:

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;
 }

我想要的是:只要未达到最大数量便会产生敌人(此部分到目前为止有效),并消灭被射击的敌人并重生另一个敌人(不起作用)。

此代码目前会创建2条鲨鱼作为敌人。 问题是当我损坏一个时,即使我朝第一个鲨鱼开枪,也只会破坏最后创建的实例。 另外,其他实例和新的产卵实例的运行状况也完全不受影响。

我将不胜感激任何建议,我花了很长时间使用这段代码,似乎我需要有关实例的一些帮助-健康逻辑。

非常感谢你

划分您的生成器管理器行为和敌人的行为,并使用界面以更可靠的方式组织代码。 将您的对象只负责一个作用域(现在,SpawnManager当前负责敌人的行为/责任)

SpawnManager应该是一个单例对象,仅具有“ maximunEnemyCount和“ currentEnemyCount ”“管理敌人产生”的maximunEnemyCount currentEnemyCount < maximunEnemyCount时,可以始终生成。

OnTakeDamage()OnDeath()应该是敌人行为的接口(在与SpawnManager分离的脚本中,假设为EnemyBehaviour脚本),并且您的destroy应该仅以其自身Destroy(this.gameObject)的实例为目标。

当您的OnDeath方法中有敌人死亡时,请记住通知SpawnManager来调整敌人当前计数。

我认为这是执行此任务的一种更优雅的方法,并且不太容易受到管理敌方实例的错误的影响。

编辑:我之前忘记的链接单例引用

您将需要一个独特的经理游戏对象,该对象应该知道可以住多少个敌人,有多少个在生活,而与敌人无关(在这种情况下,敌人的健康是敌人的责任)。

每个敌人都需要知道他/她死亡的时间,因此通知管理员减少其计数器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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