How to Instantiate platforms with certain space inbetween each platform (Endless Runner)
Hey guys,
So i am creating a platform runner (yes like the thousands out there) and need some help. I am able to get the platforms to spawn and move towards the player. Everything is going well until i figured out the platforms will need to be different lengths.
If the platforms are the same size, they spawn with a space in between each, which is great. But if for example a very long platform spawns, it takes time for it to fully move out of the spawn point. Due to the length of this long platform, another platform will spawn on top of it which is the issue.
I need some help with figuring out how i could code and spawn different length platforms without them overlapping each other and also have a randomized space in between each platform.
Here is my current code. Now i am able to change the frequency of the platforms spawning but then that will cause huge gaps in-between smaller length platforms.
{
public float scrollSpeed;
public GameObject[] obstacles;
public float frequency;
float counter = 0.0f;
//spawn point
public Transform obstacleSpawnPoint;
// Use this for initialization
void Start () {
scrollSpeed = 5;
frequency = 0.5f;
}
// Update is called once per frame
void Update () {
//instantiate object
if (counter <= 0.0f)
{
SpawnObstacle();
}
else
{
counter -= Time.deltaTime * frequency;
}
GameObject currentChild;
//this scrolls obstacles
for (int i = 0; i < transform.childCount; i++)
{
currentChild = transform.GetChild(i).gameObject;
MoveObstacle(currentChild);
if(currentChild.transform.position.x <= -15.0f)
{
Destroy(currentChild);
}
}
}
void MoveObstacle(GameObject obstacle)
{
obstacle.transform.position -= Vector3.right * (scrollSpeed * Time.deltaTime);
}
void SpawnObstacle()
{
GameObject newObstacle = Instantiate(obstacles[Random.Range(0, obstacles.Length)], obstacleSpawnPoint.position, Quaternion.identity) as GameObject;
newObstacle.transform.parent = transform;
counter = 1;
}
}
Thanks
You could store the length of each platform somewhere and make the spawntime dynamically.
Or maybe you can add an empty gameobject to each platform and place it at the end as a kind of "end marker". Then you use only this marker to detect when to spawn a new platform.
BTW: From a performance point of view it's not a good idea to instantiate new objects constantly. Use a object pool. Hide the objects that are not used and re-use them when needed.
$$anonymous$$any of the official Unity samples use object pools. Check it out.
Your answer
![](https://koobas.hobune.stream/wayback/20220612102219im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Endless - Slight gaps that I cannot explain 0 Answers
reference instantiated prefab on a scroll list? 0 Answers
Moving left and right with one button in 2d Game 1 Answer
How To Generate Platforms Down Each other in a pattern with some different positions Unity 1 Answer
Endless Runner platform Heigh generator not working. 0 Answers