Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by doesnotknowwhatheisdoing · Jun 05, 2021 at 11:23 AM · randomgenerationlevels

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Random Room Generator 1 Answer

Overlapping Tile Generation 0 Answers

How to store Transform in an Transform array ? 2 Answers

Somthing about my script i making unity hang if i set the room count any higher that 0 can't figure out what though ? 1 Answer

Randomly generated/ procedurally generated world 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges