zSpawn是瓷砖产生的z位置每个图块长66个单位有2个瓷砖,它们都是预制件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HurdleManager : MonoBehaviour
{
public GameObject[] tilePrefabs;
public float zSpawn = 0;
public float tileLength = 66;
void Start()
{
SpawnTile(0);
SpawnTile(1);
SpawnTile(1);
SpawnTile(0);
}
// Update is called once per frame
void Update()
{
}
public void SpawnTile(int tileIndex)
{
Instantiate(tilePrefabs[tileIndex],transform.forward* zSpawn, transform.rotation);
zSpawn+=tileLength;
}
}
您是否尝试过使用递归函数/协程?您可以使用递归协程,并具有一个空的变换生成点,该生成点在每次生成对象时都会移动。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HurdleManager : MonoBehaviour
{
public GameObject[] tilePrefabs;
public Transform spawnPosition = 0;
public float tileLength = 66;
private void Start ()
{
StartCoroutine(SpawnTile(0))
}
private IEnumerator SpawnTile(int tileIndex)
{
Instantiate(tilePrefabs[tileIndex], spawnPosition, Quaternion.identity);
spawnPosition.z += tileLength; // move the spawn position
yield return new WaitForSeconds(5f); // how long do you want to wait before spawning the next tile
StartCoroutine(SpawnTile(Random.Range(0, tilePrefabs.Length))); // spawn a random new tile prefab
}
}