- Home /
How to create a level generator for an infinite runner
I am a complete beginner at Unity and I am creating my 1st project. I want to make a game where you run along and avoid obstacles, but I want it to go on FOREVER! I am following this video series on YouTube. My code is almost identical to his code.
I have an idea on how this would work:
1. Figure out where [PLAYERPOSITION] + [100_ONTHEZ-AXIS] is
2. Find out how to start at the left of the "road" and generate a random number between 0 and 5. This can be the X co-ordinate for the first obstacle (relative to the road). Then repeat something like this until all 15 units of road have been used up.
3. Move [RANDOMAMOUNTOFSPACE] forwards and do it all over again.
Can someone help me make this work??!! I have barely any experience with C#. ANY pointers would be helpful!
Answer by VARUN88 · Jun 08, 2021 at 07:46 AM
Okay so first create a cube with a width, height and length of the ground object you need and drag it in the project and it is now a prefab now do the same for the obstacles in your game and create a level Manager script like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelManager : MonoBehaviour
{
//The ground prefab
public GameObject groundPrefab;
//Reference to all ground objects
private GameObject[] allGroundObjects;
// Postion where the new prefab will be instatiated
private Vector3 instantiatePos;
// Index for the all ground GameObjects
private int groundIndex;
//player for Test
public Transform player;
// Start is called before the first frame update
void Start()
{
//Initialize the level and intantiate the ground Objects
allGroundObjects = new GameObject[5];
for (int i = 0; i < 5; i++)
{
InitializeLevel();
}
}
private void Update()
{
player.Translate(Vector3.forward * Time.deltaTime * 5f);
if (player.transform.position.z > instantiatePos.z - 35)
AddGround();
}
// Locate the last ground player just crossed to the front most
void AddGround()
{
allGroundObjects[groundIndex].transform.position = instantiatePos;
instantiatePos += new Vector3(0f, 0f, 10f);
groundIndex++;
if (groundIndex >= 5)
groundIndex = 0;
}
void InitializeLevel()
{
GameObject obj = Instantiate(groundPrefab, instantiatePos, Quaternion.identity);
allGroundObjects[groundIndex] = obj;
instantiatePos += new Vector3(0f, 0f, 10f);
groundIndex++;
if (groundIndex >= 5)
groundIndex = 0;
}
}
Here I have used a player position as a reference to add the ground you this script will be very performative as you are just changing the position of previous ground Note: I have added a 10 unit in instantiatePos you add the length of the ground in the Z Axis otherwise they might overlap. another thing is add the script to a empty gameObject not the ground itself or the player.
and for the obstacles just do the same instantiate some in the start of game and Later change their position as you need or you can use Unity's Random.Range function
Hope this script gives you the idea I have kept everything basic as you are a beginner.
Your answer
![](https://koobas.hobune.stream/wayback/20220613034628im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Random Room Generator 1 Answer
Overlapping Tile Generation 0 Answers