【问题标题】:How can you load next the level after you destroy the last clone c#销毁最后一个克隆 c# 后如何加载下一个级别
【发布时间】:2016-02-19 12:57:09
【问题描述】:

我几乎完成了我的游戏,但我只是卡在了一个部分。我必须执行一些语句,说明当最后一个克隆被销毁时,下一个级别被加载。除了我不知道该怎么做。我有一个重生器,它可以生成 20 个球体的克隆体,当我与它们碰撞时,它们会消失。消灭20个分身后,我想更上一层楼。谁能帮帮我?

这是我的重生者:

using UnityEngine;
using System.Collections;

public class spawner : MonoBehaviour 
{
    public GameObject objectToSpawn;
    public int numberOfEnemies;
    private float spawnRadius = 5;
    private Vector3 spawnPosition;
    // Use this for initialization

    void Start ()
    {
        SpawnObject();  
    }

    void Update () {}

    void SpawnObject() 
    {
        for (int i= 0; i < numberOfEnemies; i++)  
        { 
            spawnPosition = transform.position + Random.insideUnitSphere * spawnRadius; 
            Instantiate(objectToSpawn, spawnPosition, Quaternion.identity);
        }
    }
}

这是我的 BoxDestroy:

using UnityEngine;
using System.Collections;

public class BoxDestroy : MonoBehaviour 
{   
    void OnTriggerEnter(Collider collider)
    {
        if (collider.gameObject.tag == "Player") 
        {
            Destroy(gameObject);
        }
    } 
}

感谢任何帮助。

【问题讨论】:

  • 每次你摧毁一个敌人时,使用一个静态变量记录当前被摧毁的数量。这是第一步!
  • 大声笑你让这听起来很容易..但是我是一个使用 Unity 和编程的认证菜鸟。有什么我可以观看的视频或学习如何做到这一点的东西吗?
  • 第一部分我相信你有能力。你做的事情比这更难。在调用 Destroy 之后,立即在 if 语句中声明一个 int 变量并递增它。至于改变水平,那是另一回事。在 Unity 网站上搜索。
  • 我现在无法提供任何代码,因为我正在使用手机,但我确信你会到达那里。
  • 使用 UnityEngine;使用 System.Collections;公共类 BoxDestroy : MonoBehaviour { int numberOfEnemies = 0 void OnTriggerEnter(Collider collider) { if (collider.gameObject.tag == "Player") { Destroy(gameObject); numberOfEnemies = i++; if(numberOfEnemies == 10){ Application.Loadlevel(0); } } } }

标签: c# unity3d clone counter destroy


【解决方案1】:
  1. Enemy 之类的球体创建标签或任何您想要的标签。
  2. BoxDestroy 类中,在销毁对象之前,计算剩余对象的数量,如果等于1,则加载另一个场景:

    using UnityEngine;
    using System.Collections;
    
    public class BoxDestroy : MonoBehaviour 
    {   
        void OnTriggerEnter(Collider collider)
        {
            if (collider.gameObject.tag == "Player") 
            {
    
                GameObject[] remainingObj = GameObject.FindGameObjectsWithTag("Enemy");
                if (remainingObj.Length == 1)
                {
                    Application.LoadLevel("name of level you want to load");
                }
    
                Destroy(gameObject);
            }
        } 
    }
    

【讨论】:

  • @Wes 欢迎您,如果有帮助,请将其标记为答案。 tnx
猜你喜欢
  • 2021-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
相关资源
最近更新 更多