简体   繁体   English

如何阻止敌人在Unity 3D中生成?

[英]How do I stop enemies from spawning in Unity 3D?

I want to stop enemies from spawning when one of them hit the "finish" tag. 我想阻止敌人中的一个击中“结束”标记时产生它。

Here's the script which spawn enemies: 这是产生敌人的脚本:

public class spawn : MonoBehaviour {

    public GameObject enemy;
    private float spawnpoint;
    public float xlimit = 12f;
    float spawnNewEnemyTimer = 1f;

    void Start()
    {

    }

    public void Update()
    {
        spawnNewEnemyTimer -= Time.deltaTime;

        if (spawnNewEnemyTimer <= 0 )
        {
            spawnNewEnemyTimer = 3;
            GameObject nemico = Instantiate(enemy);
        }
    }

Here's the script which make the enemies appear at random point and move: 这是使敌人随机出现并移动的脚本:

public class nemici : MonoBehaviour {

    float speed = 4f;

    public GameObject enemy;
    public float xlimit = 12f;
    private float currentPosition;
    public GameObject spawn;
    bool endGame = false;

    void Start()
    {
        if (endGame == false)
        {
            Vector3 newPosition = transform.position;
            newPosition.x = Random.Range(-xlimit, xlimit);
            transform.position = newPosition;
        }
        else if (endGame == true)
        {
            return;
        }
    }

    void Update()
    {
        if (endGame == false)
        {
            //per farlo muovere verso il basso
            Vector3 movimento = new Vector3(0f, -speed, 0f);    //(x, y, z)
            transform.position += movimento * Time.deltaTime;
        }
        else if (endGame == true)
        {
            return;
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {
            Destroy(enemy);
        }

        else if (other.tag == "Finish")
        {
            Debug.Log("hai perso!");
            endGame = true;
        }
      }
   }

What's wrong with this code? 此代码有什么问题? Thanks! 谢谢!

The logic that spawns new enemies is in the spawn class. 产生新敌人的逻辑在产生类中。 The logic that decides whether you should stop spawning enemies, resides on the enemy class. 决定您是否应该停止产生敌人的逻辑位于敌人的职业上。

You are succesfully setting the endgame boolean to true whenever you want enemies to stop spawning when they hit an object with the finish tag. 只要您希望敌人在击中带有finish标签的对象时停止生成,就可以成功将endgame布尔值设置为true。

void OnTriggerEnter(Collider other){
    if (other.tag == "Finish"){
        Debug.Log("hai perso!");
        endGame = true;
    }
}

Great! 大! But you are not actually using it to stop spawns. 但是您实际上并没有使用它来阻止生成。 So far this variable is local to each enemy. 到目前为止,该变量对于每个敌人都是局部的。 So if you include checks in the start and update functions, they will only activate for the specific enemy that touched touched the finish object. 因此,如果您在启动和更新功能中包括检查,则它们只会针对触碰完成物体的特定敌人而激活。 This is not what you want. 这不是您想要的。

You want the Spawn class to check the endgame variable, and if it is active, to stop spawning enemies. 您希望Spawn类检查endgame变量,如果该变量处于活动状态,则停止产生敌人。

To do this you will have to somehow be able to access the endgame variable from Spawn, What I recommend is that you make the endGame variable a member of te Spawn class. 为此,您将必须能够以某种方式从Spawn访问endgame变量,我建议您使endGame变量成为Spawn类的成员。 And that you include a reference to the Spawn in each enemy. 并且您在每个敌人中都包含对Spawn的引用。

All together it might look something like this: 一起看起来可能像这样:

public class Spawn: MonoBehaviour{
    public boolean endgame = false;
    GameObject nemico = Instantiate(Enemy);
    nemico.ParentSpawn = this
}

public class Enemy: MonoBehaviour{
    public Spawn spawn;
    void OnTriggerEnter(Collider other){
        if (other.tag == "Finish"){
            Spawn.endGame = true;
        }
    }
}

And then you can use the endgame boolean from within Spawn to check if you should keep spawning enemies. 然后您可以在Spawn中使用残局布尔值来检查是否应该继续产生敌人。

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

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