【发布时间】:2016-11-06 19:45:48
【问题描述】:
我正在开发一个包含三种不同类型敌人的统一项目。现在,我知道当游戏开始时,三个敌人中的一个都会出现。到目前为止,一切都很好。问题是,当我杀死一个敌人时,我希望这种类型的敌人一遍又一遍地生成。所以基本上,游戏从每个敌人中的一个开始,没有其他敌人出现。当我杀死其中一种类型时,那种类型会一遍又一遍地出现,直到他们可能会杀死我。这是我的代码:
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);
}
我会在我的死亡函数下调用我的敌人健康.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
}
【问题讨论】:
-
您能否包含实际生成敌人的代码?
-
@Abion47-我在最近的编辑中包含了完整的代码。
-
当你生成一个敌人时,你会在
enemy对象中生成一个预制件的实例,但是我在你的代码中没有看到将enemy重新分配给其他任何东西的任何地方。这就是为什么你只生成一种类型的敌人,因为你没有告诉你的游戏生成其他任何东西。 -
你知道如何解决这个问题吗?我刚开始使用 Unity。