- Home /
Question by
sHaWn_230 · Aug 24, 2020 at 11:00 AM ·
overlapspawning problemsgroundoverlapping
How to avoid overlapping ground spawns?,How to avoid overlapped ground spawns
Hello to all. I`m creating an endless runner game. My player is a cube that slides forward in a ground with a slippery physic material. It seems that my spawned grounds are overlapping. It blocks my player movement to move forward. What should I do to avoid overlapping my spawned grounds?
Here`s my code to my Tile Manager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TileManager : MonoBehaviour
{
public GameObject[] tilePrefabs;
public float zSpawn = 0;
public float tilelength = 270f;
public int numberOfTiles = 2;
private List<GameObject> activeTiles = new List<GameObject>();
public Transform playerTransform;
void Start()
{
for (int i = 0; i < numberOfTiles; i++)
{
if (i == 0)
SpawnTile(0);
else
SpawnTile(Random.Range(0, tilePrefabs.Length));
}
}
void Update()
{
if(playerTransform.position.z - 275 > zSpawn - (numberOfTiles * tilelength))
{
SpawnTile(Random.Range(0, tilePrefabs.Length));
DeleteTile();
}
}
public void SpawnTile(int tileIndex)
{
GameObject go = Instantiate(tilePrefabs[tileIndex], transform.forward * zSpawn, transform.rotation);
activeTiles.Add(go);
zSpawn += tilelength;
}
private void DeleteTile()
{
Destroy(activeTiles[0]);
activeTiles.RemoveAt(0);
}
}
Comment