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.

Instantiating Prefab Consecutively Set Distance Apart.

Discussion in 'Scripting' started by Zohresh, Apr 25, 2022.

  1. Zohresh

    Zohresh

    Joined:
    Apr 12, 2014
    Posts:
    7
    Hello,

    I'm working on an endless runner. I'm having an issue instantiating prefabs at a set distance apart, while my level is being generated based on 3 sections (each section is the same prefab instantiated by a list), as the player object crosses the trigger at the end of a section, the last section in line is moved to the front of the list.

    I've tried to instantiate from an object's position both attempted an object inside the prefab and outside the prefab. The problem I come across is that the way I'm spawning the object that holds the consecutive spawners "spawnPosition" moves each time the Instantiation is called by the for loop. If I decide to do what is currently set each time the trigger is crossed the objects continuously spawn further and further out away from the player until the player is no longer coming in contact with the objects.

    The SpawnLaneTrigger() method is called each time the player crosses a trigger in the middle of a section mentioned above.

    I see what the problem is and why it's happening, but I don't know what a good solution to the problem is. I've been googling like a mad man trying to come to a good solution, but I've reached what I believe is the edge of my knowledge at this time to remediate my current problem.

    Help would be greatly appreciated!

    Code (CSharp):
    1. public class LaneSpawner : MonoBehaviour
    2. {
    3.     [SerializeField] private int maxLaneCount;
    4.     [SerializeField] private GameObject spawnPosition;
    5.  
    6.     public GameObject lanes;
    7.     private Vector3 currentPosition = new Vector3(0, 1, 60);
    8.     private Vector3 offset = new Vector3(0, 0, 8);
    9.     private int distance = 3;
    10.  
    11.    
    12.  
    13.  
    14.  
    15.     public void Start()
    16.     {
    17.         spawnPosition.transform.position = new Vector3(0, 0, 0) + spawnPosition.transform.position;
    18.        
    19.     }
    20.  
    21.  
    22.  
    23.  
    24.    
    25.     public void SpawnLaneTrigger()
    26.     {
    27.         for (int i = 0; i < maxLaneCount; i++)
    28.         {
    29.             SpawnLanes();
    30.         }
    31.     }
    32.  
    33.     public void SpawnLanes()
    34.     {
    35.        
    36.         GameObject.Instantiate(lanes,currentPosition + offset, Quaternion.identity);
    37.         currentPosition.z += distance;
    38.  
    39.         /*
    40.    {
    41.        GameObject ln = GameObject.Instantiate(lanes, spawnPosition + offset, Quaternion.identity);
    42.        spawnPosition.transform.position = ln.transform.position;
    43.    }
    44.   */
    45.     }
    46. }
    47.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    33,462
    Sounds like you need to break things apart.

    - decide where to start spawning

    - store that position in a Vector3

    - for each spawn:

    ----> spawn the object
    ----> move that Vector3 forward by the proper amount
    ----> wait until it's time to spawn the next object