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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Distance based object generation.

Discussion in 'Scripting' started by Bentendo, Oct 25, 2015.

  1. Bentendo

    Bentendo

    Joined:
    Feb 23, 2015
    Posts:
    2
    Greetings all.

    I've been chipping away on a little 2D endless runner game over the past 2 weeks between work and struggling to get a firm foothold on a script I am trying to write. I have an object generator that spawns pooled objects (Gen_A) and I would like to implement another object generator with pooled objects (Gen_B). I am trying to achieve a result that will deactivate Gen_A when the player has travelled a certain distance which in turn will activate Gen_B. I have tried a number of approaches with little to no success. I appreciate if any light could shed on the subject in order to help point me in the right direction.

    Thanks.
     
  2. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Hello,
    Could you please show you object generating code?
    Please don't forget to include you code in code tags :)
     
  3. Bentendo

    Bentendo

    Joined:
    Feb 23, 2015
    Posts:
    2
    Sure thing. I should note that the Asteroid Generator is a child of the camera.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class AsteroidGenerator : MonoBehaviour {
    6.  
    7.     public GameObject theAsteroid;
    8.     public Transform generationPoint;
    9.     public float distanceBetween;
    10.  
    11.     private float asteroidWidth;
    12.  
    13.     public float distanceBetweenMin;
    14.     public float distanceBetweenMax;
    15.  
    16.     // public GameObject[] theAsteroids;
    17.     private int asteroidSelector;
    18.     private float[] asteroidWidths;
    19.  
    20.     public ObjectPooler[] theObjectPools;
    21.  
    22.     private float minHeight;
    23.     public Transform maxHeightPoint;
    24.     private float maxHeight;
    25.     public float maxHeightChange;
    26.     private float heightChange;
    27.  
    28.     // Use this for initialization
    29.     void Start ()
    30.     {
    31.         // asteroidWidth = theAsteroid.GetComponent<BoxCollider2D>().size.x;
    32.  
    33.         asteroidWidths = new float[theObjectPools.Length];
    34.  
    35.         for (int i = 0; i < theObjectPools.Length; i++)
    36.         {
    37.             asteroidWidths[i] = theObjectPools[i].pooledObject.GetComponent<BoxCollider2D>().size.x;
    38.         }
    39.  
    40.         minHeight = transform.position.y;
    41.         maxHeight = maxHeightPoint.position.y;
    42.     }
    43.  
    44.     // Update is called once per frame
    45.     void Update ()
    46.     {
    47.  
    48.         if(transform.position.x < generationPoint.position.x)
    49.         {
    50.             distanceBetween = Random.Range(distanceBetweenMin, distanceBetweenMax);
    51.  
    52.             asteroidSelector = Random.Range(0, theObjectPools.Length);
    53.  
    54.             heightChange = transform.position.y + Random.Range(maxHeightChange, -maxHeightChange);
    55.  
    56.             if(heightChange > maxHeight)
    57.             {
    58.                 heightChange = maxHeight;
    59.             }
    60.  
    61.             else if (heightChange < minHeight)
    62.             {
    63.                 heightChange = minHeight;
    64.             }
    65.  
    66.             transform.position = new Vector3(transform.position.x + (asteroidWidths[asteroidSelector] / 2) + distanceBetween, heightChange, transform.position.z);
    67.  
    68.  
    69.  
    70.             // Instantiate (/* theAsteroid */ theAsteroids[asteroidSelector], transform.position, transform.rotation);
    71.  
    72.             GameObject newAsteroid = theObjectPools[asteroidSelector].GetPooledObject();
    73.  
    74.             newAsteroid.transform.position = transform.position;
    75.             newAsteroid.transform.rotation = transform.rotation;
    76.             newAsteroid.SetActive (true);
    77.  
    78.             transform.position = new Vector3(transform.position.x + (asteroidWidths[asteroidSelector] / 2), transform.position.y, transform.position.z);
    79.  
    80.         }
    81.  
    82.     }
    83. }
    84.