Hi guys, currently developing a 2D game like an infinite runner - the difference being the platforms scroll vertically (a procedural, infinite abseiler). I wanted to have blocks of different sizes in an array, that all Translate up in unison and then recycle continuously, resizing as they do so. I’m actually quite for on with the game and this is working ok, but there has been a little niggling bug I’ve been putting off fixing…
Basically, when the platforms recycle there will sometimes be TINY, but noticeable, gaps in between some of them which ruins the illusion of this being a cliff. My question is, is this because of some flaw of logic in my script, or is this caused by floating point inaccuracy or something else?
My Script is below, and [here][1] is a link to a Dropbox download of a small test project I have created to demonstrate the problem. Notice the very small gap that appears sometimes, directly after recycling (may need to zoom in a bit). Does anyone have any suggestions?
Thanks!
using UnityEngine;
using System.Collections.Generic;
public class PlatformManager : MonoBehaviour {
public Transform prefab; // The oject to generate
public int numberOfObjects; // How many objects are required
public Vector3 minSize, maxSize, minGap, maxGap; // Maximum size of the blocks and the gaps inbetween
public float xPos = 0.0f; // Position of platform on X axis
public float vertScreenLimitTop; // Stores the Y co-ordinate of "off screen" at the top
public float vertScreenLimitBottom; // Stores the Y co-ordinate of "off screen" at the bottom
public float startYCoord = 0.0f; // Stores the coordinate to place the first block on the Y axis
public float speed = 2.0f; // Vector stores the vertical scrolling speed of the procedural structure
private Transform[] objectArray; // An array of transforms to hold the objects required.
void Start () {
Vector3 spawnPosition = new Vector3(0.0f, startYCoord, 0.0f); // Holds the set of co-ordinates to create a prefab. Where the first spawn starts from
objectArray = new Transform[numberOfObjects]; // Create a "wall" using an array of prefabs
for(int i = 0; i < numberOfObjects; i++) {
Vector3 scale = new Vector3(Random.Range((int)minSize.x, (int)maxSize.x), // Create a vector that stores random scale settings
Random.Range((int)minSize.y, (int)maxSize.y), // to be used to scale each block.
1f);
spawnPosition = new Vector3(xPos + scale.x/2, startYCoord + i + 1.0f, 0f); // Place the blocks one on top of the other, by increasing spawnPosition
objectArray *= ((Transform)Instantiate(prefab, spawnPosition, prefab.rotation)); // For each position in the array, instantiate a prefab at spawnPosition*
_ objectArray*.localScale = scale;*_
* } // y value by 1.*
* }*
* void Update () {*
* for(int i = 0; i < numberOfObjects; i++) { // in every frame, translate all objects in the array*
objectArray_.transform.Translate(0, speed * Time.deltaTime, 0f); // so they move up, at a rate of speed*delta time_
* Vector3 scale = new Vector3(Random.Range((int)minSize.x, (int)maxSize.x), // Create a vector that stores random scale settings*
* Random.Range((int)minSize.y, (int)maxSize.y), // to be used to scale each block.*
* 1f); *
_ if(objectArray*.localPosition.y >= vertScreenLimitTop) {
objectArray.localScale = scale; // Set the scale of each block in the objects array to the random scale*
objectArray*.localPosition = new Vector3 (xPos + scale.x/2, vertScreenLimitBottom, 0); // Recycle the block to the bottom of the screen, if it reaches the top*
* }
}
}*_
}
_*[1]: Dropbox - Error - Simplify your life