如何统一连续创建产卵器

How to create spawner in a row in unity

我想水平生成 3 个对象,我想随机生成它们(例如,在第一次生成时,如果蓝色在中间,接下来它将随机定位,另一个与此相同)

这是我现在的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemySpawner : MonoBehaviour {

public float min_X = -2.3f, max_X = 2.3f;

public GameObject[] colorobject;


public float timer = 2f;

void Start() {
    Invoke("SpawnObject", timer);
}

void SpawnObject() {

    float pos_X = Random.Range(min_X, max_X);
    Vector3 temp = transform.position;
    temp.x = pos_X;

    if(Random.Range(0, 3) > 0) {

        Instantiate(colorobject[Random.Range(0, colorobject.Length)],
        temp, Quaternion.identity);
        
    } else {

       //
        
    }

    Invoke("SpawnObject", timer);

}



} // class

您可以尝试使用随机整数值和范围。例如,如果我决定范围在0-6之间,如果我的数字在0和1之间,我会生成Red-Blue-Green;如果它在1和2之间,我会生成Red-Green-Blue。这可能不是最好的方法,但我现在能想到的唯一方法。你可以使用 Random.Range 作为你在上面使用它的方式:D