简体   繁体   English

Unity3D中的WaitForSeconds

[英]WaitForSeconds in Unity3D

I have an IENumerator in Unity wich activates a floor object every 0.70f-0.82f, so the gaps between the floor objects are randomly generated. 我在Unity中有一个IENumerator,它每隔0.70f-0.82f激活一个地板对象,因此地板对象之间的间隙是随机生成的。 The problem is, that when someone gets an FPS drop, the gaps are going to be huge and they player has no chance to reach the next floor. 问题是,当某人的FPS下降时,差距将会很大,而他们的玩家将没有机会到达下一层。 Can I make the "yield WaitForSeconds" fps-independent? 我可以使“ yield WaitForSeconds”与fps无关吗? This is the code: 这是代码:

IEnumerator spawnFloors () {

    while (playerControll.isAlive) {
                    for (int i = 0; i < floors.Length; i++) {

                            spawnWait = Random.Range (0.70f, 0.82f);  // 0.65f, 0.85f; aktuell = 0.70f, 0.82f;
                            Vector2 spawnPosition = new Vector2 (transform.position.x, transform.position.y);
                            Quaternion spawnRotation = Quaternion.identity;
                            for (int a = 0; a < floors.Length; a++) {
                                    int randomFloor = Random.Range (0, 9);
                                    if (!floors [randomFloor].activeInHierarchy) {
                                            floors [randomFloor].SetActive (true);
                                            break;

                                    }
                            }
                            //Instantiate(floors[Random.Range (0, 3)], spawnPosition, spawnRotation);
                            yield return new WaitForSeconds (spawnWait);


                    }
            }

    }`

Have you considered using FixedUpdate() instead? 您是否考虑过使用FixedUpdate()

In case you are wondering what's the difference , unity has a tutorial for it . 如果您想知道有什么区别 ,那么unity提供了一个教程

It's simply used for frame-independent updates in your game. 它仅用于游戏中与帧无关的更新。

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

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