Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Can You help me Fix This? (Infinite level Generator)

Discussion in 'Scripting' started by EnderSkelly34, Oct 6, 2020.

  1. EnderSkelly34

    EnderSkelly34

    Joined:
    Jun 13, 2020
    Posts:
    27
    I am making a game called Boxel Run 3D, and I am in process of making an infinite level gen. This is THE LAST THING I need to code to finish the game, but I could not fix it FOR 6 MONTHS. It kind of works, but when you are about to move from one tile to another, the tile disappears underneath your feet, and you die.

    Here is my code:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Net.NetworkInformation;
    4. using UnityEditor;
    5. using UnityEngine;
    6. using UnityEngine.PlayerLoop;
    7.  
    8. public class LevelController : MonoBehaviour
    9. {
    10.     public LevelPiece[] levelPieces;
    11.     public int DrawDistance;
    12.     public float pieceLength;
    13.     public Transform _camera;
    14.     float currentcamStep;
    15.     float lastcamStep;
    16.     public float ttw = 1.0f;
    17.     public float done = 0.0f;
    18.     public float speed;
    19.  
    20.     Queue<GameObject> activePieces = new Queue<GameObject>();
    21.     List<int> probabilityList = new List<int>();
    22.     private void Start() {
    23.         BuildProbabilityList();
    24.         for (int i = 0; i < DrawDistance; i++) {
    25.  
    26.               SpawnNewPieces();
    27.  
    28.         }
    29.         currentcamStep = (int)((_camera.transform.position.z + 3.15) / pieceLength);
    30.         lastcamStep = currentcamStep;
    31.     }
    32.     private void Update() {
    33.         _camera.transform.position = Vector3.MoveTowards(_camera.transform.position, _camera.transform.position + Vector3.forward, Time.deltaTime * speed);
    34.         currentcamStep = (int)(_camera.transform.position.z / pieceLength);
    35.         if (currentcamStep != lastcamStep) {
    36.            
    37.             DespawnPiece();
    38.             lastcamStep = currentcamStep;
    39.             SpawnNewPieces();
    40.            
    41.         }
    42.     }
    43.     void SpawnNewPieces() {
    44.         int pieceIndex = probabilityList[UnityEngine.Random.Range(0, probabilityList.Count)];
    45.         GameObject newLevelPiece = Instantiate(levelPieces[pieceIndex].prefab, new Vector3(0f, 0f, pieceLength * (currentcamStep + activePieces.Count) * pieceLength), Quaternion.identity);
    46.         activePieces.Enqueue(newLevelPiece);
    47.     }
    48.     void DespawnPiece() {
    49.  
    50.         //wait for seconds?
    51.         GameObject oldLevelPiece = activePieces.Dequeue();
    52.         Destroy(oldLevelPiece);
    53.  
    54.     }
    55.  
    56.     void BuildProbabilityList() {
    57.  
    58.         int index = 0;
    59.         foreach (LevelPiece piece in levelPieces) {
    60.  
    61.             for (int i = 0; i< piece.probability; i++) {
    62.  
    63.                 probabilityList.Add(index);
    64.  
    65.             }
    66.             index++;
    67.         }
    68.  
    69.     }
    70. }
    71. [System.Serializable]
    72. public class LevelPiece {
    73.     public string name;
    74.     public GameObject prefab;
    75.     public int probability = 1;
    76. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    I think only you can fix this because only you know what it's supposed to actually do, and how all the pieces in this script fit into your game.

    If you have a specific issue you're encountering that you can explain with enough context for an outsider to understand, maybe someone on the forums can chime in and help.
     
  3. EnderSkelly34

    EnderSkelly34

    Joined:
    Jun 13, 2020
    Posts:
    27
    If this helps, when I edit the variables current can step and lastcamstep, as well as piece length, if gets closer and closer to fixing, although unfortunately this is as close as i've gotten.
     
  4. EnderSkelly34

    EnderSkelly34

    Joined:
    Jun 13, 2020
    Posts:
    27
    I am trying to instantiate the pices and then destroy then once the player leaves them.
     
  5. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    If it is an endless runner, don't destroy them, just disable them and move them in front of the camera and enable them again when required.
     
  6. EnderSkelly34

    EnderSkelly34

    Joined:
    Jun 13, 2020
    Posts:
    27
    Long abandoned project. Working on different ones now.