我正在尝试创建一个“Endless runner”游戏,其中玩家在轴的原点上保持静止,而瓷砖在实例化时在 Z 轴上以固定的速度滚动。附加的脚本在开始时生成 5 个图块,同时已经以 GameManager 中设置的速度滚动,然后根据固定的生成速率继续生成图块。
我面临的问题是在实例化前五个之后计算瓷砖之间的正确距离,以便瓷砖的每一端都重叠。
我在 TileManager 脚本中附上了我到目前为止所写的内容,希望有人知道如何继续,或者可能知道不同的方法。谢谢!
public class TileManager : MonoBehaviour
{
[SerializeField] private Transform player;
[SerializeField] private GameObject[] tilesArray;
[SerializeField] private int tilesToSpawn = 5;
[SerializeField] private float zPosSpawn = 0f;
[SerializeField] private float tileLength = 20f;
[SerializeField] private float scrollCoefficient;
private float timeSinceLastSpawned;
private float spawnRate;
void Start()
{
//scrollingObjectsSpeed = -1,5f
spawnRate = GameManager.Instance.scrollingObjectsSpeed * -1;
SpawnLogic();
}
void Update()
{
timeSinceLastSpawned += Time.deltaTime;
if (timeSinceLastSpawned >= spawnRate)
{
timeSinceLastSpawned = 0;
SpawnTiles(Random.Range(1, tilesArray.Length));
}
}
private void SpawnLogic()
{
for (int i = 0; i < tilesToSpawn; i++)
{
if (i == 0)
{
SpawnTiles(0);
}
else
{
SpawnTiles(Random.Range(0, tilesArray.Length));
}
}
}
private void SpawnTiles(int tileIndex)
{
scrollCoefficient = (GameManager.Instance.scrollingObjectsSpeed * -1f) * spawnRate ;
Instantiate(tilesArray[tileIndex], transform.forward * zPosSpawn, Quaternion.identity);
zPosSpawn = (zPosSpawn + tileLength) - scrollCoefficient;
}
}
`