Search Unity

Making a Infinite runner infinite?

Discussion in 'Getting Started' started by MadsLeander, Jan 18, 2019.

  1. MadsLeander

    MadsLeander

    Joined:
    Jun 9, 2018
    Posts:
    2
    Hello, I am quit a noobi in unity and coding in general, so I am sorry for my lack of knowledge. But currently I wanted to make an infinite runner, I have the more or less most things I need to have in the game, but I am still missing the endless part of the runner. I did try to make a script, it wasn't anything great but it was something, its spawned prefabs in front of the character as he went forward. But the only problem I have is that I just doesn't seem to be able to have multiple prefabs randomly spawn in front of the player. it instead only takes the first prefab and always used that, can you see the problem with my horribly written script?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GroundManagerScript : MonoBehaviour {
    6.  
    7.     public GameObject[] GroundPrefabs;
    8.  
    9.     private Transform playerTransform;
    10.     private float spawnZ = 50f;
    11.     private float groundLength = 50f;
    12.     private int amnGroundsOnScreen = 5;
    13.  
    14.     private void Start ()
    15.     {
    16.         playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
    17.  
    18.         for (int i = 0; i < amnGroundsOnScreen; i++)
    19.         {
    20.             SpawnGround();
    21.         }
    22.     }
    23.  
    24.     private void Update ()
    25.     {
    26.         if (playerTransform.position.z > (spawnZ - amnGroundsOnScreen * groundLength))
    27.         {
    28.             SpawnGround();
    29.         }
    30.     }
    31.  
    32.     private void SpawnGround(int prefabIndex = -1)
    33.     {
    34.         GameObject go;
    35.         go = Instantiate (GroundPrefabs[0]) as GameObject;
    36.         go.transform.SetParent (transform);
    37.         go.transform.position = Vector3.forward * spawnZ;
    38.         spawnZ += groundLength;
    39.     }
    40. }
    41.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    On line 35 you have hardcoded to use index 0 of the GroundPrefabs array, this will mean only the first prefab in the array will ever be used. You probably want to use the provided prefabIndex. But you should also specify the prefabIndex whenever calling SpawnGround, because using a value of -1 will generate an out of index range error (there is never an index of -1 in an array).
     
  3. MadsLeander

    MadsLeander

    Joined:
    Jun 9, 2018
    Posts:
    2
    oh... I totally forgot that I hard coded those numbers for testing :D Thank you so much! :) + I also don't know why I used -1 as an index, that's just proof of my terrible coding.
     
    Joe-Censored likes this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Just looks like you're new to this, rather than being terrible. I find when I hit confusing issues like this it is best to take a break, come back with fresh eyes, and try to walk through the code visually. Also, using a liberal number of Debug.Log statements that outputs every value used can also help.
     
  5. JapaneseBreakfast

    JapaneseBreakfast

    Joined:
    Sep 7, 2018
    Posts:
    44
    I'm also trying to make an infinite runner and this video actually helped a lot. It's 22 minutes, but it will teach you how to repeat multiple backgrounds and give each "layer" of backgrounds unique parallax speeds.

    Using the code provided in the video, I was able to generate new enemies before the recycled background appeared in front of the player. Considering you're making a 2D game.

    Hope it helps.