Search Unity

How to instantiate prefabs on x axis of player for an infinite level

Discussion in 'Scripting' started by Lewgriffs, Jan 5, 2020.

  1. Lewgriffs

    Lewgriffs

    Joined:
    Aug 13, 2019
    Posts:
    1
    Hi,

    I am very new to coding and Unity and I have created a small infinite game. It works well and the player moves and has to avoid obstacles that randomly spawn from prefabs that I have built.

    The coding will instantiate a new prefab when the player runs out of 'safe space' ahead of itself and remove the old prefabs to save on memory space.

    I would like the level to be infinite on the x axis too so that players can move left and right in any direction and spawn prefabs in all directions.

    My code currently checks if it needs to spawn tiles on the z axis and then runs the commands so I think its probably easier to do the same checks separately for x axis to the left and again for the right but i can't seem to get the coding right.

    Any thoughts?

    L

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using System.Collections.Generic;
    4.  
    5. public class GameManager : MonoBehaviour
    6. {
    7.  
    8.     // Boolean to understand if the game has ended or not
    9.     bool gameHasEnded = false;
    10.  
    11.     // Float to manage the delay before restarting the game
    12.     public float restartDelay = 1f;
    13.  
    14.     public GameObject completeLevelUI;
    15.  
    16.     public GameObject[] tilePrefabs;    // Creating an array for our prefabs
    17.  
    18.     public Transform playerTransform;   // Creates a field to identify our player
    19.  
    20.    
    21.     // Floats to manage the spawning of tiles moving forward
    22.     public float spawnz = 0.0f;
    23.     public float tileLength = 100.0f;
    24.     public float safeZone = 110.0f;
    25.     public int amnTilesOnScreen = 3;
    26.     public int lastPrefabIndex = 0;
    27.  
    28.     public List<GameObject> activeTilesZ;
    29.  
    30.     // This code is the spawn ground at beginning of the game
    31.     public void Start()
    32.     {
    33.         activeTilesZ = new List<GameObject>();   // Create a list of all tiles that are spawned
    34.         for (int i = 0; i < amnTilesOnScreen; i++)
    35.  
    36.             if (i < 2)
    37.                 SpawnTile(0);
    38.             else
    39.                 SpawnTile();
    40.     }
    41.  
    42.  
    43.  
    44.     // This code is called once per frame
    45.     public void Update()
    46.     {
    47.         if (playerTransform.position.z - safeZone > (spawnz - amnTilesOnScreen * tileLength))
    48.         {
    49.             SpawnTile();
    50.             DeleteTile();
    51.         }
    52.     }
    53.  
    54.     public void DeleteTile()
    55.     {
    56.         Destroy(activeTilesZ[0]);
    57.         activeTilesZ.RemoveAt(0);
    58.     }
    59.  
    60.     public void SpawnTile(int prefabIndex = -1)
    61.     {
    62.         GameObject go;
    63.         if (prefabIndex == -1)
    64.             go = Instantiate(tilePrefabs[RandomPrefabIndex()]) as GameObject;
    65.         else
    66.             go = Instantiate(tilePrefabs[prefabIndex]) as GameObject;
    67.         go.transform.SetParent(transform);
    68.         go.transform.position = Vector3.forward * spawnz;
    69.         spawnz += tileLength;
    70.         activeTilesZ.Add(go);
    71.     }
    72.  
    73.     public int RandomPrefabIndex()
    74.     {
    75.         if (tilePrefabs.Length <= 1)
    76.             return 0;
    77.  
    78.         int randomIndex = lastPrefabIndex;
    79.         while (randomIndex == lastPrefabIndex)
    80.         {
    81.             randomIndex = Random.Range(0, tilePrefabs.Length);
    82.         }
    83.  
    84.         lastPrefabIndex = randomIndex;
    85.         return randomIndex;
    86.     }