我无尽的刷出地形有时刷一次,有时刷两次.为什么

本文关键字:一次 两次 为什么 无尽 | 更新日期: 2023-09-27 17:53:47

我使用Unity 3D引擎制作一款2D游戏。我想创造无限重复的地形。我让它无缝地重复,除了有时两个地形会同时刷出。它是随机发生的,没有任何好的理由。任何帮助吗?

这是我的地形重生代码:

using UnityEngine;
using System.Collections;
using PlayerPrefs = GamePlayerPrefs;
public class SelfTerrainSpawn : MonoBehaviour {
    // terrain references
    public GameObject first;
    public GameObject second;
    public GameObject third;
    public GameObject fourth;
    public GameObject fifth;
    public GameObject sixth;
    public GameObject seventh;
    public float spawnXPos = 0.0f;
    public float respawnCoordinate = 30.4f;
    public float respawnTriggerCoordinate = -21.7f;
    public bool canSpawn = true;
    public bool starting = true;
    public float random;
    void Start() {
        this.gameObject.transform.position = new Vector2 (0.0f, 31.4f);
        this.gameObject.transform.eulerAngles = new Vector3 (0, 0, 90.0f);
    }

    void Update()
    {
        // if the camera is farther than the number last position minus 16 terrain is spawned
        // a lesser number may make the terrain 'pop' into the scene too early
        // showing the player the terrain spawning which would be unwanted
        if (this.gameObject.transform.position.y <= respawnTriggerCoordinate && canSpawn)
        {
            // turn off spawning until ready to spawn again
            random = Random.Range(0,18);
            SpawnTerrain (random);
            canSpawn = false;
        }
        if (this.gameObject.transform.position.y <= respawnTriggerCoordinate - 10) {
            Destroy (this.gameObject);
        }
    }
    void SpawnTerrain(float rand) {
        if ((rand == 0)) {
            Instantiate (first, new Vector3 (respawnCoordinate, spawnXPos, 0), Quaternion.Euler (0, 0, 0));
        }
        if ((rand >= 1) && (rand <= 4)) {
            Instantiate (second, new Vector3 (respawnCoordinate, spawnXPos, 0), Quaternion.Euler (0, 0, 0));
        }
        if ((rand >= 5) && (rand <= 8)) {
            Instantiate (third, new Vector3 (respawnCoordinate, spawnXPos, 0), Quaternion.Euler (0, 0, 0));
        }
        if ((rand >= 9) && (rand <= 10)) {
            Instantiate (fourth, new Vector3 (respawnCoordinate, spawnXPos, 0), Quaternion.Euler (0, 0, 0));
        }
        if ((rand >= 10) && (rand <= 13)) {
            Instantiate (fifth, new Vector3 (respawnCoordinate, spawnXPos, 0), Quaternion.Euler (0, 0, 0));
        }
        if ((rand >= 13) && (rand <= 15)) {
            Instantiate (sixth, new Vector3 (respawnCoordinate, spawnXPos, 0), Quaternion.Euler (0, 0, 0));
        }
        if ((rand >= 15) && (rand <= 18)) {
            Instantiate (seventh, new Vector3 (respawnCoordinate, spawnXPos, 0), Quaternion.Euler (0, 0, 0));
        }
    }
}

这个脚本附加到第一个地形块,在游戏内部:

点击这里查看游戏画面

它也附在另外两个预制件上。每个预制件都有一个脚本,可以让它们在屏幕上缓慢移动。基本上,当一个顶点到达摄像机顶部时,另一个应该在摄像机上方生成。然后向下移动并重复。看到我使用的canSpawn变量,以确保它只产卵一次。然而,在随机情况下,两个地形会在彼此之上生成。谁能给我一个解决这个问题的方法?

我无尽的刷出地形有时刷一次,有时刷两次.为什么

rand为10,13或15时,您将生成两个地形,因为您的范围检查与这些值重叠。