C# 统一3D-僵尸死后如何重生?
我正在尝试制作一个简单的3D fps。现在,我正在研究随机繁殖的僵尸。唯一的问题是,在僵尸产卵后,我射杀了它们,它们不会重生。例如,我如何使它在10秒后出现另一组僵尸 以下是我的僵尸产卵器脚本:C# 统一3D-僵尸死后如何重生?,c#,unity3d,C#,Unity3d,我正在尝试制作一个简单的3D fps。现在,我正在研究随机繁殖的僵尸。唯一的问题是,在僵尸产卵后,我射杀了它们,它们不会重生。例如,我如何使它在10秒后出现另一组僵尸 以下是我的僵尸产卵器脚本: using System.Collections; using UnityEngine; public class GenerateEnemies : MonoBehaviour { public GameObject theEnemy; public float xPos; public float
using System.Collections;
using UnityEngine;
public class GenerateEnemies : MonoBehaviour
{
public GameObject theEnemy;
public float xPos;
public float zPos;
public int enemyCount;
// Start is called before the first frame update
void Start()
{
StartCoroutine(EnemyDrop1());
StartCoroutine(EnemyDrop2());
StartCoroutine(EnemyDrop3());
StartCoroutine(EnemyDrop4());
}
IEnumerator EnemyDrop1()
{
while(enemyCount < 5)
{
xPos = Random.Range(-22, -130);
zPos = Random.Range(135, -135);
Instantiate(theEnemy, new Vector3(xPos, 1, zPos), Quaternion.identity);
yield return new WaitForSeconds(0.1f);
enemyCount += 1;
}
}
IEnumerator EnemyDrop2()
{
while (enemyCount < 10)
{
xPos = Random.Range(-23, 134);
zPos = Random.Range(-75, -135);
Instantiate(theEnemy, new Vector3(xPos, 1, zPos), Quaternion.identity);
yield return new WaitForSeconds(0.1f);
enemyCount += 1;
}
}
IEnumerator EnemyDrop3()
{
while (enemyCount < 15)
{
xPos = Random.Range(30, 130);
zPos = Random.Range(-75, 135);
Instantiate(theEnemy, new Vector3(xPos, 1, zPos), Quaternion.identity);
yield return new WaitForSeconds(0.1f);
enemyCount += 1;
}
}
IEnumerator EnemyDrop4()
{
while (enemyCount < 20)
{
xPos = Random.Range(-22, -130);
zPos = Random.Range(135, -135);
Instantiate(theEnemy, new Vector3(xPos, 1, zPos), Quaternion.identity);
yield return new WaitForSeconds(0.1f);
enemyCount += 1;
}
}
}
使用系统集合;
使用UnityEngine;
公共阶级世代成员:单一行为
{
公众游戏对象的敌人;
公共浮动XPO;
公共浮动zPos;
公共国际电子账户;
//在第一帧更新之前调用Start
void Start()
{
start例程(EnemyDrop1());
start例程(EnemyDrop2());
start例程(EnemyDrop3());
start例程(EnemyDrop4());
}
IEnumerator EnemyDrop1()
{
while(enemyCount<5)
{
xPos=随机范围(-22,-130);
zPos=随机范围(135,-135);
实例化(theEnemy,newvector3(xPos,1,zPos),四元数.identity);
收益率返回新WaitForSeconds(0.1f);
enemyCount+=1;
}
}
IEnumerator EnemyDrop2()
{
while(enemyCount<10)
{
xPos=随机范围(-23134);
zPos=随机范围(-75,-135);
实例化(theEnemy,newvector3(xPos,1,zPos),四元数.identity);
收益率返回新WaitForSeconds(0.1f);
enemyCount+=1;
}
}
IEnumerator EnemyDrop3()
{
while(enemyCount<15)
{
xPos=随机范围(30130);
zPos=随机范围(-75135);
实例化(theEnemy,newvector3(xPos,1,zPos),四元数.identity);
收益率返回新WaitForSeconds(0.1f);
enemyCount+=1;
}
}
IEnumerator EnemyDrop4()
{
while(enemyCount<20)
{
xPos=随机范围(-22,-130);
zPos=随机范围(135,-135);
实例化(theEnemy,newvector3(xPos,1,zPos),四元数.identity);
收益率返回新WaitForSeconds(0.1f);
enemyCount+=1;
}
}
}
顺便说一句,我这样做的脚本,以便僵尸不会产卵太接近玩家
我是unity的新手,所以如果你有答案,请解释一下。谢谢:)为什么僵尸不会重新繁殖
关于你的僵尸停止重生,有几个潜在的原因:
enemyCount
中减去任何内容。假设其他地方也是如此,它可能是问题的罪魁祸首解决方案 我将提供的一个潜在解决方案是使while循环永远持续或使用不同的条件,并将以前使用的条件放入if语句中。这将使循环保持功能,但跳过了当僵尸太多时产生僵尸的步骤。
以下是您的代码的修改版本:
using System.Collections;
using UnityEngine;
public class GenerateEnemies : MonoBehaviour
{
public GameObject theEnemy;
public float xPos;
public float zPos;
public int enemyCount;
private void Start()
{
StartCoroutine(EnemyDrop1());
StartCoroutine(EnemyDrop2());
StartCoroutine(EnemyDrop3());
StartCoroutine(EnemyDrop4());
}
private IEnumerator EnemyDrop1()
{
while (true)
{
if (enemyCount >= 5)
{
yield return new WaitForSeconds(0.1f);
continue;
}
xPos = Random.Range(-22, -130);
zPos = Random.Range(135, -135);
Instantiate(theEnemy, new Vector3(xPos, 1, zPos), Quaternion.identity);
yield return new WaitForSeconds(0.1f);
enemyCount += 1;
}
}
private IEnumerator EnemyDrop2()
{
while (true)
{
if (enemyCount >= 10)
{
yield return new WaitForSeconds(0.1f);
continue;
}
xPos = Random.Range(-23, 134);
zPos = Random.Range(-75, -135);
Instantiate(theEnemy, new Vector3(xPos, 1, zPos), Quaternion.identity);
yield return new WaitForSeconds(0.1f);
enemyCount += 1;
}
}
private IEnumerator EnemyDrop3()
{
while (true)
{
if (enemyCount >= 15)
{
yield return new WaitForSeconds(0.1f);
continue;
}
xPos = Random.Range(30, 130);
zPos = Random.Range(-75, 135);
Instantiate(theEnemy, new Vector3(xPos, 1, zPos), Quaternion.identity);
yield return new WaitForSeconds(0.1f);
enemyCount += 1;
}
}
private IEnumerator EnemyDrop4()
{
while (true)
{
if (enemyCount >= 20)
{
yield return new WaitForSeconds(0.1f);
continue;
}
xPos = Random.Range(-22, -130);
zPos = Random.Range(135, -135);
Instantiate(theEnemy, new Vector3(xPos, 1, zPos), Quaternion.identity);
yield return new WaitForSeconds(0.1f);
enemyCount += 1;
}
}
}
下面是同一代码的稍微重构版本:
using System.Collections;
using UnityEngine;
public class GenerateEnemies : MonoBehaviour
{
public GameObject theEnemy;
public float xPos;
public float zPos;
public int enemyCount;
private void Start()
{
StartCoroutine(EnemyDrop(5, -130f, -22f, -135f, 135f));
StartCoroutine(EnemyDrop(10, -23f, 134f, -135f, -75f));
StartCoroutine(EnemyDrop(15, 30f, 130f, -75f, 135f));
StartCoroutine(EnemyDrop(20, -130f, -22f, -135f, 135f));
}
private IEnumerator EnemyDrop(
int maxEnemyCount,
float minPositionX,
float maxPositionX,
float minPositionZ,
float maxPositionZ)
{
while (true)
{
if (enemyCount >= maxEnemyCount)
{
yield return new WaitForSeconds(0.1f);
continue;
}
xPos = Random.Range(minPositionX, maxPositionX);
zPos = Random.Range(minPositionZ, maxPositionZ);
Instantiate(theEnemy, new Vector3(xPos, 1, zPos), Quaternion.identity);
enemyCount += 1;
yield return new WaitForSeconds(0.1f);
}
}
}
附加说明
如果您打算编写一个解决方案,希望逐渐生成10个僵尸,然后停止,直到再次没有僵尸,我将给您留下以下代码示例作为提示(我将把完整的解决方案留给您):
private IEnumerator keepdropping敌人()
{
while(true)
{
产生返回新的等待时间(()=>enemyCount==0);
收益率(10);
}
}
私有IEnumerator DropOfEquaries(int numberOfEquaries)
{
for(int i=0;i
不清楚您的要求。。你想让每一个僵尸在被杀死10秒后重生吗?或者你想等到所有僵尸都被杀死,然后再等10秒,然后实例化一个新的整波吗?是的,这是正确的。。。。哪一个?我只是问了两个不同的问题^^哦,对了,在我杀死所有僵尸后,我等了10秒钟,然后我想让脚本重新执行。如果你知道我的意思
private IEnumerator KeepDroppingEnemies()
{
while (true)
{
yield return new WaitUntil(() => enemyCount == 0);
yield return DropEnemies(10);
}
}
private IEnumerator DropEnemies(int numberOfEnemies)
{
for (int i = 0; i < numberOfEnemies; i++)
{
// Spawn an enemy
yield return new WaitForSeconds(0.1f);
}
}