Search Unity

Resolved Level generator with multiple endpoints

Discussion in 'Scripting' started by Faasje, Jun 5, 2022.

  1. Faasje

    Faasje

    Joined:
    Jun 2, 2022
    Posts:
    3
    Hi there,

    I was working on my first project, which would be an endless runner style game. For generating the endless level I copied over this script from YouTube and it is working great.

    It generates a new random level part at the endpoint of the previous one based on the distance of the player.

    However, I wanted to expand on this by having level parts that split up like a fork in the road and would have multiple endpoints. I have tried to work it out myself, without success.

    The fact that not all level parts would have this additional endpoint(s) seems to complicate the matter as well.

    Anyone who has an idea how I could fix this?

    This is the script for one endpoint:
    Code (CSharp):
    1. public class LevelGenerator : MonoBehaviour
    2. {
    3.  
    4.     private const float playerDistance = 200f;
    5.     [SerializeField] private Transform levelStart;
    6.     [SerializeField] private List<Transform> levelPartList;
    7.     [SerializeField] private Player player;
    8.  
    9.     private Vector2 lastEndPosition;
    10.  
    11.     private void Awake()
    12.     {
    13.         lastEndPosition = levelStart.Find("EndPosition").position;
    14.         SpawnLevelPart();
    15.         SpawnLevelPart();
    16.         SpawnLevelPart();
    17.  
    18.     }
    19.  
    20.     private void Update()
    21.     {
    22.         if (Vector2.Distance(player.transform.position, lastEndPosition) < playerDistance)
    23.         {
    24.             SpawnLevelPart();
    25.         }
    26.  
    27.     }
    28.  
    29.     private void SpawnLevelPart()
    30.     {
    31.         Transform chosenLevelPart = levelPartList[Random.Range(0, levelPartList.Count)];
    32.         Transform lastLevelPartTransform = SpawnLevelPart(chosenLevelPart, lastEndPosition);
    33.         lastEndPosition = lastLevelPartTransform.Find("EndPosition").position;
    34.     }
    35.  
    36.     private Transform SpawnLevelPart(Transform levelPart,Vector2 spawnPosition)
    37.     {
    38.         Transform levelPartTransform = Instantiate(levelPart, spawnPosition, Quaternion.identity);
    39.         return levelPartTransform;
    40.     }
    41.  
    42. }
     
    Last edited: Jun 5, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Do you intend to fork the level into two (or more) paths?

    If so then you would need to refactor it so that it could handle adding extra paths, perhaps by forking into two separate "LevelGenerator" instances running side by side.

    It might also prove easier to manage all the separate "threads" of level yourself, so that:

    a) you can keep them spaced properly

    b) you can control each sub-branch branching again or not

    c) you can bring branches back together into one branch

    Another cheese way might be to cause the above to insert a Y-forked chunk, and then switch over to using only two-path chunks after that point, and each chunk would contain the side-by-side parts necessary.
     
  3. Faasje

    Faasje

    Joined:
    Jun 2, 2022
    Posts:
    3
    Thanks for the response, turns out that was a great idea!

    I put a prefab levelgenerator in every level part which can generate one next level part. For level parts with multiple endpoints I just use multiple levelgenerators.

    The problem now is that it keeps generating new level parts also for branches of the path the player didn't take, which creates an exponential amount of new branches and makes the game lag pretty bad.

    One problem down, a new one pops up. But I'm sure I'll figure it out. Thanks man!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    After you take a branch, have some way to tell the other branch to just stop generating...

    OR... make a given branch ONLY keep generating as long as the player is close vertically?

    Lotsa of ways, each with tradeoffs.