我不断生成的地形有时生成一次,有时生成两次。为什么?
My endlessly spawning terrain sometimes spawns once and sometimes spawns twice. Why?
我正在使用 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));
}
}
}
此脚本附加到游戏内部的第一个地形块:
Click here to see an image of the game screen
它也附加到其他两个预制件上。这些预制件中的每一个都有一个脚本,可以让它们在屏幕上缓慢移动。基本上,当一个人的顶部到达相机顶部时,另一个应该在相机上方生成。然后它向下移动并重复。看到我使用了 canSpawn 变量来确保它只生成一次。然而,两个地形会随机生成在彼此之上。谁能给我解决这个问题的方法?
当 rand
为 10、13 或 15 时,您将生成两个地形,因为您的范围检查与这些值重叠。
我正在使用 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));
}
}
}
此脚本附加到游戏内部的第一个地形块:
Click here to see an image of the game screen
它也附加到其他两个预制件上。这些预制件中的每一个都有一个脚本,可以让它们在屏幕上缓慢移动。基本上,当一个人的顶部到达相机顶部时,另一个应该在相机上方生成。然后它向下移动并重复。看到我使用了 canSpawn 变量来确保它只生成一次。然而,两个地形会随机生成在彼此之上。谁能给我解决这个问题的方法?
当 rand
为 10、13 或 15 时,您将生成两个地形,因为您的范围检查与这些值重叠。