【问题标题】:Method to stop enemies from spawning Unity5阻止敌人生成 Unity5 的方法
【发布时间】:2017-11-28 09:18:53
【问题描述】:

我正在开发一款激光防御游戏。

这是负责生成敌人的主要函数。我将变量 wave 设置为 3。然后我想破坏太空船的位置,以免它们产生。问题是当我开始游戏时根本没有船。

if (AllMembersDead())
{
    for (float i = 0; i < waves; i++)
    {
        SpawnUntilFull();       // number of waves to be spawned
    }
    Destroy(position);
}

这是我销毁的游戏对象的图片:

如果需要,这里是 SpawnUntilFull() 和 AllMembersDead() 的函数。

bool AllMembersDead()
{
    foreach(Transform childPositionGameObject in transform)
    {
        if (childPositionGameObject.childCount > 0)
        {
            return false;
        }
    }
    return true;
}

void SpawnUntilFull()
{
    Transform freePosition = NextFreePosition();
    if (freePosition)
    {
        GameObject enemy = Instantiate(enemyPrefab, freePosition.transform.position, Quaternion.identity) as GameObject; // Instantiate (spawning or creating) enemy prefab (as gameobject assures that what its returning to us is no normal object but rather a game object)
        enemy.transform.parent = freePosition; // the transform of whatever thing the enemy is attached to, and that would be the enemyFormation GameObject
    }

    if (NextFreePosition())
    {
        Invoke("SpawnUntilFull", spawnDelay);
    }
}

我不确定我做错了什么。任何指导将不胜感激。

【问题讨论】:

  • 在更新功能中。

标签: c# unity3d unity5


【解决方案1】:

我认为您的 AllMembersDead() 函数返回 false,因为您正在查看的 childPositionGameObject 是 TotalPositions 游戏对象,因此 SpawnUntilFull()永远不会被调用。 尝试将函数更改为指向正确的父级,如下所示:

bool AllMembersDead()
{
    foreach(Transform childPositionGameObject in position)
    {
        if (childPositionGameObject.childCount > 0)
        {
            return false;
        }
    }
    return true;
}

【讨论】:

  • 我明白了,我想我明白你的意思了我最终在 foreach 语句上方添加了一行代码 Transform allChildren = GetComponentInChildren();不幸的是,这并没有改变任何东西。
猜你喜欢
  • 1970-01-01
  • 2013-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
  • 1970-01-01
  • 2013-03-10
  • 2017-10-04
相关资源
最近更新 更多