Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我的无止境产卵地形有时产卵一次,有时产卵两次。为什么?_C#_Unity3d_Rigid Bodies - Fatal编程技术网

C# 我的无止境产卵地形有时产卵一次,有时产卵两次。为什么?

C# 我的无止境产卵地形有时产卵一次,有时产卵两次。为什么?,c#,unity3d,rigid-bodies,C#,Unity3d,Rigid Bodies,我正在使用Unity 3D引擎制作2D游戏。我想创造无限重复的地形。我让它无缝地重复,只是有时候两个地形会同时繁殖。它是随机发生的,没有任何好的理由。有什么帮助吗 这是我的地形重生代码: using UnityEngine; using System.Collections; using PlayerPrefs = GamePlayerPrefs; public class SelfTerrainSpawn : MonoBehaviour { // terrain references

我正在使用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));
        }
    }
}
使用UnityEngine;
使用系统集合;
使用PlayerPrefs=GamePlayerPrefs;
公共类自地球兵:单行为{
//地形参考
公共游戏对象优先;
公共游戏对象第二;
公共游戏对象第三;
公共游戏对象第四;
公共游戏对象第五;
公共游戏对象;
公开游戏对象第七;
公共浮点数xpos=0.0f;
公众浮存再融资坐标=30.4f;
公共浮动再融资利率=-21.7f;
public bool canSpawn=true;
公共bool启动=真;
公共浮动随机;
void Start(){
this.gameObject.transform.position=新矢量2(0.0f,31.4f);
this.gameObject.transform.eulerAngles=新矢量3(0,0,90.0f);
}
无效更新()
{
//如果相机距离上一个位置减去16的数字,则生成地形
//较少的数字可能会使地形过早地“弹出”到场景中
//向玩家显示不需要的地形生成

如果(this.gameObject.transform.position.y当
rand
为10、13或15时,您将生成两个地形,因为您的范围检查重叠了这些值。

哦,非常感谢!这不是唯一解决的问题,您必须使用
IF ELSEIF
而不仅仅是
IF
…hi@MilesKim您在吗?作为新用户,它是imp很抱歉,它告诉我必须等2分钟,然后我就偏离了方向