Endless - Slight gaps that I cannot explain
Hi, I have been learning Unity and having a lot of fun with it.
I made an Endless Runner where the player moved along the Z axis and the track was instantiated further and further from origin - That was great and I had no discrepancies in position at all with that -Despite online reading that I would as it got further away from origin.
I thought I'd give it a go to make the track move towards me and re-instantiate and whilst it was a bit of a thought process to get it going, I have managed it.
My issues are:
Firstly, I'd love to know if there's a tried and tested algorithm for figuring out the tile spawns when they are instantiated and moving towards the player - I don't have an algorithm and if I change tile lengths etc, I have to re figure it out, which isn't going well.
Secondly, I am getting some small gaps, but only like every third tile.
I noticed that sometimes my tiles, even if hardcoded to spawn at 25f, would spawn at 24.8 or whatever.
Here is my script:
public class TileManager : MonoBehaviour
{
public GameObject[] tilePrefabs;
private Transform playerTransform;
private float spawnZ = 5f;
private float tileLength = 80f;
private int tileCount = 4;
private float laneScrollSpeed = -10f;
private float trueSpawnZ;
private int lastTileIndexUsed = 0;
float maxSpeed = -50f;
private List<GameObject> activeTiles;
// Start is called before the first frame update
private void Start()
{
// Where any tiles that aren't in the initial run should spawn
trueSpawnZ = 235f;
Debug.Log(trueSpawnZ);
// Initialise List of Tiles currently active on screen
activeTiles = new List<GameObject>();
// Find player and assign it's position for use
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
// Initial Intiial Tiles
for (int i = 0; i < tileCount; i++)
{
// Pass true to let it know it's the first set
SpawnTile(true);
}
}
// Update is called once per frame
private void Update()
{
// If center of plane is past the center of the player
// Let's make this the center of the plane + half so it's the top face that's behind the player
Debug.Log((activeTiles[0].transform.position.z + 75.2f) + " Tile.Z + 75");
if (activeTiles[0].transform.position.z + 80f <= playerTransform.transform.position.z)
{
SpawnTile();
DeleteTile();
}
}
void FixedUpdate()
{
// Control movement of the lane, (0, 0, scrollSpeed)
transform.Translate(0, 0, laneScrollSpeed * Time.deltaTime);
}
int count = 0;
private void SpawnTile(bool isFirstRun = false)
{
GameObject tile;
// Instantiate a tile
tile = Instantiate(tilePrefabs[RandomTileSpawn()]) as GameObject; //RandomTileSpawn()
// Set the parent transform as it's own
tile.transform.SetParent(transform);
// Set the tile initial spawn position;
tile.transform.position = new Vector3(0, 0, spawnZ);
Debug.Log(tile.transform.position.z + " = Pos.Z on Created Object " + count);
// If its the first run and we're generating 3 tiles, adjust the spawnz position
if (isFirstRun)
{
spawnZ += tileLength;
}
else { spawnZ = trueSpawnZ; }
activeTiles.Add(tile);
count++;
}
private void DeleteTile()
{
Destroy(activeTiles[0]);
activeTiles.RemoveAt(0);
}
private int RandomTileSpawn()
{
// Get the length of the collection
var totalTiles = tilePrefabs.Length;
// Select a tile for use
var selectedTile = Random.Range(0, totalTiles);
// Check the tile isn't the same one we just used, if it is, change it.
while (selectedTile == lastTileIndexUsed)
{
// Try again
selectedTile = Random.Range(0, totalTiles);
}
// Update the last used index.
lastTileIndexUsed = selectedTile;
// Return next index
return selectedTile;
}
}
On the first run I create 4 tiles and as the track moves past the player, I am spawning a new tile at the end and deleting the one at [0].
Initial spawn is totally fine, this is where my issue comes in. My unfinished track (The length is 80f and the center point is 5f on each one - I don't know how to make it the actual center)
The TileManager GameObject itself is at 0,0,0
I've basically got this all wrong somewhere, if anyone can shed some light on what I'm trying to do?
Thanks alot and if any more information is required, let me know and I'll get it asap!
Your answer
Follow this Question
Related Questions
How to Instantiate platforms with certain space inbetween each platform (Endless Runner) 0 Answers
Scrolling Animation Background For Endless Runner 0 Answers
my background is not scrolling why? 1 Answer
Scrollable world map with mouse drag or finger 0 Answers
Instantiating once in an update 1 Answer