Random Generator with collision detector of objects in endless running game....H3lp

Hello folks,

I want to know the following :

-How to randomly generate the game objects like buildings in an endless running game.
-Lets say I have 10 different buildings and I want them to generate randomly again and again (after some time) in
one axis like x, and I don’t want them to overlap, what should I do, that’s really important.

Summary:

I want buildings to randomly place without overlapping each other, each time user starts the game within one axis,
and reuse them again and again as in TEMPLE RUN, it should be in a way to overcome the draw calls for a mobile game.

I know I am asking too much, I tried this link but it didn’t work, please help me out.

Link I tried: Runner, a Unity C# Tutorial

The way I would do it is keep a running x value of where to place buildings. You update this value every time you place a building with the extents of your new buildings collider. This ensures that they never overlap.

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

	public Transform player;
	
	public float playerSpeed = 10;

 	public GameObject[] buildingList;
	
	//Your running x placement value. This gets modified every time you place a building so you'll always know where the next one should go.
	float buildingPlacement = 0;

    void Start () {
		PlaceBuilding();
    }

    void Update () {
		player.Translate(Time.deltaTime * playerSpeed, 0, 0);
		
		//Every time the player gets to within a certain distance from you running x value, you know you need to place a new building.
		if(buildingPlacement - player.position.x < 30)
			PlaceBuilding();
    }
	
	void PlaceBuilding () {
		//First you make the new building
		GameObject newBuilding = (GameObject)Instantiate(buildingList[Random.Range(0, buildingList.Length)]);
		//Then you get the x extent of the building's collider
		Vector3 extents = newBuilding.collider.bounds.extents;
		//Add that to your running x placement value
		buildingPlacement += extents.x;
		//And place your building at that location. Using extents also allows you to place it at the correct y value
		newBuilding.transform.position = new Vector3(buildingPlacement, extents.y, newBuilding.transform.position.z);
		//And finally add the extent value to your running x placement again plus some random value (if you want a gap in-between your buildings). You do this because the extent value is only half the width of your building.
		buildingPlacement += extents.x + Random.Range(0f, 3f);
	}
}

But I have multiple of buildings, this script is useable to place one type of building, whats the solution…

One thing you could do is have an array and randomly select your buildings from that array and then instantiate them using your array variable with a Random.Range

If you need more help i can show u the code i used for my game and how i got it to spawn different things with a rarity as well.

The “buildingList” array is for multiple building types.

Hi
Im asking my question from naked_chicken.
I have used ur code because im beginner but I want to build the objects in Z Axis so I changed some place of the code but it didnt work in z Axis and it was wotking in x Axis too.
How can I use this code to build the objects in z Axis?
And how can I use this code to have more distance between building objects?
Thanks for helping me.

Hi
Im asking my question from naked_chicken.
I have used ur code because im beginner but I want to build the objects in Z Axis so I changed some place of the code but it didnt work in z Axis and it was wotking in x Axis too.
How can I use this code to build the objects in z Axis?
And how can I use this code to have more distance between building objects?
Thanks for helping me.

Hi
Im asking my question from naked_chicken.
I have used ur code because im beginner but I want to build the objects in Z Axis so I changed some place of the code but it didnt work in z Axis and it was wotking in x Axis too.
How can I use this code to build the objects in z Axis?
And how can I use this code to have more distance between building objects?
Thanks for helping me.