统一-在第一波被击败后生成敌人的问题
本文关键字:后生 敌人 问题 统一 | 更新日期: 2023-09-27 18:16:30
我正在开发一个具有三种不同类型敌人的unity项目。现在,当游戏开始时,三个敌人中的一个将会出现。到目前为止,一切顺利。问题在于,当我杀死一个敌人时,我希望这种类型的敌人能够不断出现。所以基本上,游戏一开始只有一个敌人,没有其他敌人出现。当我杀死一种类型时,这种类型会反复出现,直到它们可能杀死我。下面是我的代码:
public bool waveOne = true;
public float subTime = .5f; //Lab 11 work
#endregion Member Variables
#region Member Functions
void Start ()
{
if (waveOne == true) {
Invoke ("Spawn", spawnTime);
}
else {
// Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
InvokeRepeating ("Spawn", spawnTime, spawnTime);
}
}
上面的函数会生成每种类型的一个。
我尝试创建这个函数来调用重复的敌人:
public void callEnemies()
{
waveOne = false;
InvokeRepeating ("Spawn", spawnTime, spawnTime);
}
我会在我的死亡函数下的enemyHealth.cs中调用callEnemies。
public void Death ()
{
// The enemy is dead.
isDead = true;
// Turn the collider into a trigger so shots can pass through it.
capsuleCollider.isTrigger = true;
EnemyManager.Instance.callEnemies();
// Tell the animator that the enemy is dead.
anim.SetTrigger ("Dead");
// Change the audio clip of the audio source to the death clip and play it (this will stop the hurt clip playing).
enemyAudio.clip = deathClip;
enemyAudio.Play ();
#region Lab10 Addition
// Kill the Enemy (remove from EnemyList)
// Get the game object associated with "this" EneymHealth script
// Then get the InstanceID of that game object.
// That is the game object that needs to be killed.
EnemyManager.Instance.Kill(this.gameObject.GetInstanceID());
#endregion // Lab10 Addition
}
所以现在我的三个敌人中只有一个出来了。但另外两个没有。任何帮助都太好了!
使用System.Collections.Generic;使用UnityEngine;
公共类EnemyManager: MonoBehaviour{#region成员变量
#region LAB10 Addition
public static EnemyManager Instance
{
get { return EnemyManager._Instance; }
}
private static EnemyManager _Instance = null;
public EnemyManager()
{
if (EnemyManager._Instance == null) EnemyManager._Instance = this;
}
public Dictionary<int, Object> EnemyList = new Dictionary<int, Object>();
#endregion // LAB10 Addition
public PlayerHealth playerHealth; // Reference to the player's heatlh.
public GameObject enemy; // The enemy prefab to be spawned.
public float spawnTime = 3f; // How long between each spawn.
public Transform[] spawnPoints; // An array of the spawn points this enemy can spawn from.
public bool waveOne = true;
public float subTime = .5f; //Lab 11 work
#endregion Member Variables
#region Member Functions
void Start ()
{
if (waveOne == true) {
Invoke ("Spawn", spawnTime);
}
else {
// Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
InvokeRepeating ("Spawn", spawnTime, spawnTime);
}
}
void Spawn ()
{
// If the player has no health left...
if(playerHealth.currentHealth <= 0f)
{
// ... exit the function.
return;
}
// Find a random index between zero and one less than the number of spawn points.
int spawnPointIndex = Random.Range (0, spawnPoints.Length);
#region LAB10 Addition
// Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
// Original Code (next line) did not capture the resulting object.
// Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
Object o = Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
EnemyManager.Instance.EnemyList.Add(o.GetInstanceID(), o);
#endregion // LAB10 Addition
#region Lab 11
((GameObject)o).GetComponent<EnemyHealth>().EnemyHealthHandler += ScoreManager.EnemyHealthEvent;
#endregion
}
#region Lab10 Addition
public void Kill(int InstanceID)
{
if (EnemyManager.Instance.EnemyList.ContainsKey(InstanceID))
EnemyManager.Instance.EnemyList.Remove(InstanceID);
//Lab 11 notes
//bool IsAlive = (playerHealth.currentHealth <= 0f) ? true : false;
}
public void DeathToAll()
{
Dictionary<int, Object> TempEnemyList = new Dictionary<int, Object>(EnemyManager.Instance.EnemyList);
foreach (KeyValuePair<int, Object> kvp in TempEnemyList)
{
// kvp.Key; // = InstanceID
// kvp.Value; // = GameObject
GameObject go = (GameObject)kvp.Value;
go.GetComponent<EnemyHealth>().Death();
}
}
#endregion // Lab10 Addition
public void callEnemies()
{
waveOne = false;
InvokeRepeating ("Spawn", spawnTime, spawnTime);
}
#endregion Member Functions
}
如果我正确理解了你的意图,你的陷阱是在你的"Spawn"方法
void Spawn ()
{
if(playerHealth.currentHealth <= 0f)
{
return;
}
int spawnPointIndex = Random.Range (0, spawnPoints.Length);
Object o = Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation); // only one
EnemyManager.Instance.EnemyList.Add(o.GetInstanceID(), o);
((GameObject)o).GetComponent<EnemyHealth>().EnemyHealthHandler += ScoreManager.EnemyHealthEvent;
}
你只实例化了一个敌人的预制加载到一个游戏对象。我们没有足够的代码来编写一个完整的解决方案,但似乎你想要实例化所有三个类型,并跟踪哪个类型被杀死以产生更多的该类型。
编辑:看了你的评论后,我意识到为什么它会卡在一个敌人身上。查看单例:
public static EnemyManager Instance
{
get { return EnemyManager._Instance; }
}
private static EnemyManager _Instance = null;
public EnemyManager()
{
if (EnemyManager._Instance == null) EnemyManager._Instance = this;
}
当你创建第一个敌人时,_instance被设置为"this"。当你试图创建第二个敌人时,if语句为假,敌人的类型仍然是第一个。
静态_instance对于脚本的所有实例都是一样的