在一个时间间隔上生成精灵,并防止它们重叠。

问题描述 投票:-1回答:1

我是一个unity和游戏开发的新手,所以也许这个问题真的很基本,但我找不到如何解决它。首先我想解释一下我的游戏。有一个球,我们控制和这个球改变其颜色后,它通过一堵墙。这堵墙是由3部分组成,分别是红色,蓝色和黄色。这是一个无限的游戏,你必须通过相同颜色的球。我把墙放入不同的层中,并使它们成为预制板。这是我的代码来生成墙的部分。

  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();
            }


    }
    }

当我开始游戏时,墙的部分就会产生。

像这样

然而,他们应该在一个间隔,这是这3个部分的总长度,他们不应该重叠.也应该改变颜色的顺序。

像这样

c# unity3d unityscript
1个回答
0
投票

问题是你是完全随机的位置的墙壁上的屏幕上

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    
}
© www.soinside.com 2019 - 2024. All rights reserved.