简体   繁体   English

我如何设置最大值。 现有预制件的数量,并且在玩家通过它们后还会在 Unity 中消失预制件地板?

[英]How do i set the max. amount of existing prefabs and also despawn Prefabs floors in Unity after the player passes them?

So here is the code, right now i only spawn each frame x Prefabs (Its a simple Game im new to unity so the player is a ball and dodges stuff etc. So i have an amount of prefabs (Floors) that is spawning in front of the palyer)所以这里是代码,现在我只生成每帧 x 预制件(它是一个简单的游戏,我对统一很陌生,所以玩家是一个球并躲避东西等。所以我有大量的预制件(地板)在前面产生播放器的)


    public static int numSpawned = 0;
    //Creates an Array to store PreFabs into it
    public static GameObject[] thePath;
    //Sets the x value of the spawning postion to 100
    public int SpawningDistance = 100;
    
    public int numToSpawn = 6;

//Calls the PreFabs Folder
    private void Start()
    {
        thePath = Resources.LoadAll<GameObject>("PreFabs");
    }
    //Sets the Spawn Position and ++ the numSpawned
    void SpawnRandomObject()
    {
        int withItem = Random.Range(0,6);

        GameObject myObj = Instantiate(thePath[withItem]) as GameObject;

        numSpawned++;

        myObj.transform.position = transform.position + new Vector3(0, 0, 0);
    }

    //Til now it Spawns every Frame so thats what you have to fix
    void Update()
    {   //compares the Values
        if (numToSpawn > numSpawned)
        {
            //where your object spawns from
            transform.position = new Vector3(SpawningDistance, 0, 0);
            SpawnRandomObject();

            SpawningDistance = SpawningDistance + 100;
        }
    }

Til now its the problem that i dont know how to controlle the spawning amount and speed so i need help for that.直到现在它的问题是我不知道如何控制产卵量和速度,所以我需要帮助。 All Prefabs are beeing stored in an array and then spawned randomly out of it.所有预制件都存储在一个数组中,然后随机生成。

Instead of using method SpawnRandomObject() every frame, you could create coroutine and yield WaitForSeconds(value).您可以创建协程并产生 WaitForSeconds(value),而不是每帧都使用方法SpawnRandomObject() )。

[SerializeField] private float delaySpawnInSeconds = 0.5f;

...

private void Start()
{ 
    thePath = Resources.LoadAll<GameObject>("PreFabs");
    StartCoroutine(SpawnFloors());
}

Our coroutine:我们的协程:

private IEnumerator SpawnFloors()
{
    while (numToSpawn > numSpawned)
    {
       //SpawnRandomObject();  //if U want to spawn object then wait
       yield return new WaitForSeconds(delaySpawnInSeconds);
       SpawnRandomObject();  //if U want to wait then spawn object
    }
}

Doc: https://docs.unity3d.com/Manual/Coroutines.html文档: https://docs.unity3d.com/Manual/Coroutines.html

To get spawning amount, you could use Captain Skyhawk's idea to:要获得产卵量,您可以使用天鹰船长的想法:

Keep an array of spawned objects and check the count.保留一组生成的对象并检查计数。

Arrays: Doc: https://docs.unity3d.com/2018.4/Documentation/ScriptReference/Array-length.html Arrays: Doc: https://docs.unity3d.com/2018.4/Documentation/ScriptReference/Array-length.html

You can also use lists, for example:您还可以使用列表,例如:

private List<GameObject> exampleList = new List<GameObject>();
private int sizeOfExampleList;

...\\Add items to the list in the code.\\...

private void exampleMethod () 
{
    sizeOfExampleList = exampleList.Count;
}

...

I hope I helped You: :-)我希望我能帮助你:-)

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

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