C# 间隔产卵精灵并防止它们重叠
在我的游戏中,有一个球是我们控制的,这个球穿过墙后会改变颜色。墙由红、蓝、黄三部分组成。这个游戏玩起来就像一只扑翼鸟。这是一个无限的游戏,你必须通过与球相同的颜色。我把墙分为不同的层,并把它们做成一个预制件 以下是我生成部分墙的代码:C# 间隔产卵精灵并防止它们重叠,c#,unity3d,C#,Unity3d,在我的游戏中,有一个球是我们控制的,这个球穿过墙后会改变颜色。墙由红、蓝、黄三部分组成。这个游戏玩起来就像一只扑翼鸟。这是一个无限的游戏,你必须通过与球相同的颜色。我把墙分为不同的层,并把它们做成一个预制件 以下是我生成部分墙的代码: using System.Collections; using System.Collections.Generic; using UnityEngine; public class deployWalls : MonoBehaviour {
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class deployWalls : MonoBehaviour
{
public GameObject blue;
public GameObject yellow;
public GameObject red;
public float rewpawnTime=1.0f;
private Vector2 screenBounds;
// Start is called before the first frame update
void Start()
{
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3 (Screen.width,Screen.height,Camera.main.transform.position.z));
StartCoroutine(wallWave());
}
private void spawnWall()
{
GameObject b = Instantiate(blue) as GameObject;
GameObject y = Instantiate(yellow) as GameObject;
GameObject r = Instantiate(red) as GameObject;
b.transform.position = new Vector2(screenBounds.x *-2, Random.Range(-screenBounds.y, screenBounds.y));
y.transform.position = new Vector2(screenBounds.x *-2, Random.Range(-screenBounds.y, screenBounds.y));
r.transform.position = new Vector2(screenBounds.x *-2, Random.Range(-screenBounds.y, screenBounds.y));
}
IEnumerator wallWave()
{
while(true){
yield return new WaitForSeconds(2);
spawnWall();
}
}
}
当我开始游戏时,墙壁部分会产生:
但是,它们的间隔应该是这三部分的总长度,并且它们不应该重叠。颜色的顺序也应该改变
问题在于,您完全是在随机化屏幕上墙壁的位置
b.transform.position = new Vector2(screenBounds.x *-2, Random.Range(-screenBounds.y, screenBounds.y));
y.transform.position = new Vector2(screenBounds.x *-2, Random.Range(-screenBounds.y, screenBounds.y));
r.transform.position = new Vector2(screenBounds.x *-2, Random.Range(-screenBounds.y, screenBounds.y));
你需要做的是首先随机排列颜色的顺序。我们还需要墙的高度
float wallHeight = 5; //arbitrary number for example purpose
int orderBlue = 0;
int orderRed = 0;
int orderYellow = 0;
do {
colorBlue = Random.Range(0, 2);
colorRed = Random.Range(0, 2);
colorYellow = Random.Range(0, 2);
} while ((orderBlue == orderRed) || (orderBlue == orderYellow) || (orderRed = orderYellow))
首先确定要使用的颜色,然后在下面添加其他墙
if (colorBlue == 2) {
b.transform.position = new Vector2(screenBounds.x *-2, Random.Range(-screenBounds.y, screenBounds.y));
if (colorYellow == 1) {
y.transform.position = new Vector2(b.transform.position.x, b.transform.position.y - wallHeight);
r.transform.position = new Vector2(b.transform.position.x, b.transform.position.y - 2 * wallHeight);
}
if (colorRed == 1) {
r.transform.position = new Vector2(b.transform.position.x, b.transform.position.y - wallHeight);
y.transform.position = new Vector2(b.transform.position.x, b.transform.position.y - 2 * wallHeight);
}
}
if (colorRed == 2) {
// add same code as above but switch colors
}
if (colorYellow == 2) {
// add same code as above but switch colors
}
如何获取墙的高度您可以尝试r.GetComponent().size.y;初始化r后,您也可以尝试r.GetComponentChildren().size.y,我不确定预制件是如何设置的,您还可以在inspector中体验设置浮动,看看如果上述操作不起作用,效果如何,只需在wallHeight前面添加public即可