Search Unity

New and not understanding please help

Discussion in 'Scripting' started by amichael, Mar 7, 2015.

  1. amichael

    amichael

    Joined:
    Mar 4, 2015
    Posts:
    48
    I want to get rid of this random generator and spawn my obstacles in a set order i have created. Can anyone walk me through this? im new to all of this and Unity forums has been my learning tool, and i am really grateful for all the help i have been receiving.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3. using System.Collections;
    4.  
    5. public class ObstacleGeneration : MonoBehaviour {
    6.  
    7.     public GameObject highBrickPrefab, lowBrickPrefab, fencePrefab;
    8.  
    9.     public float recycle;
    10.     public float minZ, maxZ;
    11.     public int numberOfObjects;
    12.     private Vector3 lowPos, highPos, fPos;
    13.     public float lowBrickProbability, highBrickProbability, fenceProbability;
    14.     private Vector3 nextPos;
    15.     private Queue<Transform> obstacleQueue;
    16.     private Transform player;
    17.     private int curObst;
    18.     private int curQue = 1;
    19.     private bool start = true;
    20.     private bool nextCreate = false;
    21.     private bool waitCreate = true;
    22.  
    23.  
    24.  
    25.  
    26.     void Start()
    27.     {
    28.  
    29.  
    30.         player = GameObject.FindGameObjectWithTag("Player").transform;
    31.         highPos = new Vector3(3.083166f, 1.491387f, player.position.z);
    32.         lowPos = new Vector3(3.083166f, 0.2214425f, player.position.z);
    33.         carPos = new Vector3(4.687229f, 0.0924263f, player.position.z);
    34.         obstacleQueue = new Queue<Transform>(numberOfObjects);
    35.         Creation();
    36.         for(int i = 0; i < numberOfObjects; i++)
    37.         {
    38.             Recycle();
    39.         }
    40.         start=false;
    41.     }
    42.  
    43.     private void Creation()
    44.     {
    45.      
    46.         for(int i = 0; i < numberOfObjects; i++)
    47.         {
    48.             curObst = Random.Range(0,100);
    49.  
    50.             if(curObst <= lowBrickProbability)
    51.             {
    52.                 obstacleQueue.Enqueue((Transform) Instantiate(highBrickPrefab.transform));
    53.                 if(i == 0 && start)
    54.                 {
    55.                     nextPos = highPos;
    56.                 }
    57.             }
    58.              
    59.             else if(curObst <= highBrickProbability && curObst > lowBrickProbability)
    60.             {
    61.                 obstacleQueue.Enqueue((Transform) Instantiate(lowBrickPrefab.transform));
    62.                 if(i == 0 && start)
    63.                 {
    64.                     nextPos = lowPos;
    65.                 }
    66.             }
    67.              
    68.             else if(curObst <= fenceProbability && curObst > highBrickProbability)
    69.             {
    70.                 obstacleQueue.Enqueue((Transform) Instantiate(fencePrefab.transform));
    71.                 if(i == 0 && start)
    72.                 {
    73.                     nextPos =fPos;
    74.                 }
    75.             }
    76.          
    77.             else if(curObst <= fenceProbability && curObst > fenceProbability)
    78.             {
    79.                 obstacleQueue.Enqueue((Transform) Instantiate(longcarPrefab.transform));
    80.                 if(i == 0 && start)
    81.                 {
    82.                     nextPos =fPos;
    83.                 }
    84.             }
    85.          
    86.             else
    87.             {
    88.                 Debug.Log ("BREAK");
    89.             }
    90.          
    91.         }
    92.     }
    93.  
    94.     private void Recycle(float extraZ = 0f)
    95.     {
    96.         Transform o = obstacleQueue.Dequeue();
    97.         if(o.name == highBrickPrefab.name+"(Clone)")
    98.         {
    99.             nextPos = new Vector3(highPos.x,highPos.y,nextPos.z);
    100.          
    101.         }
    102.         else if(o.name == lowBrickPrefab.name+"(Clone)")
    103.         {
    104.             nextPos = new Vector3(lowPos.x,lowPos.y,nextPos.z);
    105.         }
    106.         else
    107.         {
    108.             nextPos = new Vector3(fPos.x,fPos.y,nextPos.z);
    109.         }
    110.      
    111.         nextPos += new Vector3(
    112.             0f,
    113.             0f,
    114.             Random.Range((0-minZ)+extraZ, (0-maxZ)+extraZ));
    115.      
    116.         Vector3 position = nextPos;
    117.         o.localPosition = position;
    118.         obstacleQueue.Enqueue(o);
    119.     }
    120.  
    121.     void Update()
    122.     {
    123.         if(curQue <= numberOfObjects && !nextCreate)
    124.         {
    125.             waitCreate = true;
    126.             if(obstacleQueue.Peek().localPosition.z - recycle > player.position.z)
    127.             {
    128.                 Destroy(GameObject.Find (obstacleQueue.Peek().name));
    129.                 obstacleQueue.Dequeue();
    130.                 curQue++;
    131.             }
    132.         }
    133.         else
    134.         {
    135.             nextCreate = true;
    136.             StartCoroutine(Wait(0.5f));
    137.             curQue = 1;
    138.         }
    139.     }
    140.  
    141.     private void GameStart()
    142.     {
    143.         nextPos = transform.localPosition;
    144.         for(int i = 0; i < numberOfObjects; i++)
    145.         {
    146.             Recycle();
    147.         }
    148.      
    149.     }
    150.  
    151.     IEnumerator Wait(float duration)
    152.     {
    153.         if(waitCreate)
    154.         {
    155.             waitCreate = false;
    156.             yield return new WaitForSeconds(duration);   //Wait
    157.             Creation();
    158.             for(int i = 0; i < numberOfObjects; i++)
    159.             {
    160.                 if(i == 0)
    161.                 {
    162.                     Recycle(-25f);
    163.                 }
    164.                 else { Recycle(); }
    165.             }
    166.             nextCreate = false;
    167.         }
    168.  
    169.     }
    170. }
    171.  
     
  2. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Can you make a new script from scratch? Or do you have to modify this script? When I paste the code into a new script I get lots of errors. I might be missing a reference script also.
     
  3. amichael

    amichael

    Joined:
    Mar 4, 2015
    Posts:
    48
    @roger0 i dont mind starting from scratch. What would you suggest doing to get this done? I have been trying to do this for a few days now.
     
  4. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    557
    I'm not sure what you mean.. or are intending to do.

    This line is creating the random objects:
    Code (CSharp):
    1. curObst = Random.Range(0,100);
    Can you not simply pass an int to Creation and set curObst to it?

    Code (CSharp):
    1. private void Creation(int someNumber) {
    2. curObjst = someNumber;
    3. .
    4. .
    5. .
    6. // curObst = Random.Range(0,100);
    7.  
     
  5. amichael

    amichael

    Joined:
    Mar 4, 2015
    Posts:
    48
    @spryx ya, everyone has been telling me they are not sure what i am trying to do. I appreciate you responding, and ya i understand that is the line creating the random generation. Here is an example of what i want to do. So as you know i have a random generator. When i press play on the game it generates, object a,g,f,e,d,c,etc (in a random order). What i want is to have them spawn in a patter i have made for the level. For example i want level 1 to be a,b,c,d,e,f,g,h. hope that helps. This code was originally done by a buddy of mine who wanted my help, but he said he didnt want to work on this anymore and so i decided to finish it. I guess this is where the mixup occurs for me.
     
  6. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    557
    I need to know how your are storing the patterns in order to help.
     
  7. amichael

    amichael

    Joined:
    Mar 4, 2015
    Posts:
    48
    @spryx man i feel horrible, so i am new to unity and if you mean where are they located, they are in my assets folder. I hope i am not annoying any of you guys, i just started some classes in programming so i know i can be a bit confusing.
     
  8. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    557
    Its ok, we are all new at some point. I mean how your are storing the pattern in terms of data
    eg:
    100,50,40,20,80,20
    or as arrays or ....
     
  9. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    No worries. What kind of game is it? It would help to get a picture of whats going on in the scene.
     
  10. amichael

    amichael

    Joined:
    Mar 4, 2015
    Posts:
    48
    @spryx ok so in the inspector next to the names it says 25,50,75,100.. if this isnt what you mean im so sorry man, just point me in the direction and ill let you know asap.
     
  11. amichael

    amichael

    Joined:
    Mar 4, 2015
    Posts:
    48
    @roger0 its a runner, my character is just running and jumping over certain obstacles and going under obstacles, and earning points along the way. Your average runner, my first game i want to try and finish and learn from.
     
  12. amichael

    amichael

    Joined:
    Mar 4, 2015
    Posts:
    48
    I am willing to scrap all of that script he has created and start over.
     
  13. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    557
    ok, im working on something for you. give me a couple
     
  14. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    557
    ok, try this

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4. using System.Collections;
    5.  
    6. public class ObstacleGeneration : MonoBehaviour {
    7.  
    8.     public GameObject highBrickPrefab, lowBrickPrefab, fencePrefab;
    9.  
    10.     public float recycle;
    11.     public float minZ, maxZ;
    12.     public int numberOfObjects;
    13.     private Vector3 lowPos, highPos, fPos;
    14.     public float lowBrickProbability, highBrickProbability, fenceProbability;
    15.     private Vector3 nextPos;
    16.     private Queue<Transform> obstacleQueue;
    17.     private Transform player;
    18.     private int curObst;
    19.     private int curQue = 1;
    20.     private bool start = true;
    21.     private bool nextCreate = false;
    22.     private bool waitCreate = true;
    23.  
    24.     public bool createRandom = false;
    25.     public int[] pattern;
    26.  
    27.     void Start()
    28.     {
    29.         if (pattern.Length != numberOfObjects) {
    30.             Debug.LogError("You must have " + numberOfObjects + " integers in your pattern array!");
    31.             Debug.LogError("there are currently " + pattern.Length);
    32.             Application.Quit();
    33.         }
    34.  
    35.         player = GameObject.FindGameObjectWithTag("Player").transform;
    36.         highPos = new Vector3(3.083166f, 1.491387f, player.position.z);
    37.         lowPos = new Vector3(3.083166f, 0.2214425f, player.position.z);
    38.         carPos = new Vector3(4.687229f, 0.0924263f, player.position.z);
    39.         obstacleQueue = new Queue<Transform>(numberOfObjects);
    40.         Creation();
    41.         for(int i = 0; i < numberOfObjects; i++)
    42.         {
    43.             Recycle();
    44.         }
    45.         start=false;
    46.     }
    47.  
    48.     private void Creation()
    49.     {
    50.    
    51.         for(int i = 0; i < numberOfObjects; i++)
    52.         {
    53.             if (createRandom) {
    54.                 curObst = Random.Range(0,100);
    55.             }
    56.             else {
    57.                 curObst = pattern[i];
    58.             }
    59.        
    60.             if(curObst <= lowBrickProbability)
    61.             {
    62.                 obstacleQueue.Enqueue((Transform) Instantiate(highBrickPrefab.transform));
    63.                 if(i == 0 && start)
    64.                 {
    65.                     nextPos = highPos;
    66.                 }
    67.             }
    68.        
    69.             else if(curObst <= highBrickProbability && curObst > lowBrickProbability)
    70.             {
    71.                 obstacleQueue.Enqueue((Transform) Instantiate(lowBrickPrefab.transform));
    72.                 if(i == 0 && start)
    73.                 {
    74.                     nextPos = lowPos;
    75.                 }
    76.             }
    77.        
    78.             else if(curObst <= fenceProbability && curObst > highBrickProbability)
    79.             {
    80.                 obstacleQueue.Enqueue((Transform) Instantiate(fencePrefab.transform));
    81.                 if(i == 0 && start)
    82.                 {
    83.                     nextPos =fPos;
    84.                 }
    85.             }
    86.        
    87.             else if(curObst <= fenceProbability && curObst > fenceProbability)
    88.             {
    89.                 obstacleQueue.Enqueue((Transform) Instantiate(longcarPrefab.transform));
    90.                 if(i == 0 && start)
    91.                 {
    92.                     nextPos =fPos;
    93.                 }
    94.             }
    95.        
    96.             else
    97.             {
    98.                 Debug.Log ("BREAK");
    99.             }
    100.        
    101.         }
    102.     }
    103.  
    104.     private void Recycle(float extraZ = 0f)
    105.     {
    106.         Transform o = obstacleQueue.Dequeue();
    107.         if(o.name == highBrickPrefab.name+"(Clone)")
    108.         {
    109.             nextPos = new Vector3(highPos.x,highPos.y,nextPos.z);
    110.        
    111.         }
    112.         else if(o.name == lowBrickPrefab.name+"(Clone)")
    113.         {
    114.             nextPos = new Vector3(lowPos.x,lowPos.y,nextPos.z);
    115.         }
    116.         else
    117.         {
    118.             nextPos = new Vector3(fPos.x,fPos.y,nextPos.z);
    119.         }
    120.    
    121.         nextPos += new Vector3(
    122.             0f,
    123.             0f,
    124.             Random.Range((0-minZ)+extraZ, (0-maxZ)+extraZ));
    125.    
    126.         Vector3 position = nextPos;
    127.         o.localPosition = position;
    128.         obstacleQueue.Enqueue(o);
    129.     }
    130.  
    131.     void Update()
    132.     {
    133.         if(curQue <= numberOfObjects && !nextCreate)
    134.         {
    135.             waitCreate = true;
    136.             if(obstacleQueue.Peek().localPosition.z - recycle > player.position.z)
    137.             {
    138.                 Destroy(GameObject.Find (obstacleQueue.Peek().name));
    139.                 obstacleQueue.Dequeue();
    140.                 curQue++;
    141.             }
    142.         }
    143.         else
    144.         {
    145.             nextCreate = true;
    146.             StartCoroutine(Wait(0.5f));
    147.             curQue = 1;
    148.         }
    149.     }
    150.  
    151.     private void GameStart()
    152.     {
    153.         nextPos = transform.localPosition;
    154.         for(int i = 0; i < numberOfObjects; i++)
    155.         {
    156.             Recycle();
    157.         }
    158.    
    159.     }
    160.  
    161.     IEnumerator Wait(float duration)
    162.     {
    163.         if(waitCreate)
    164.         {
    165.             waitCreate = false;
    166.             yield return new WaitForSeconds(duration);   //Wait
    167.             Creation();
    168.             for(int i = 0; i < numberOfObjects; i++)
    169.             {
    170.                 if(i == 0)
    171.                 {
    172.                     Recycle(-25f);
    173.                 }
    174.                 else { Recycle(); }
    175.             }
    176.             nextCreate = false;
    177.         }
    178.    
    179.     }
    180. }
    181.  
    182.  
    in the inspector, you should have two new things a create random check box and an array called pattern. If you want to create a random pattern, check that box and pattern array will be ignored.
    If you want a preset pattern, fill in the field in the pattern in the order you want. These need to correspond to how your objects are created, so for example, if you want a highBrick then
    the field should be an integer from 0 to lowBrickProbability.

    Also remember that you need the length of your pattern to match the number of objects. So if that is 5, then you need 5 different integers in the pattern array. no more, no less.

    PM me if you are having trouble with this.

    I should mention that I have no way to test it.
     
    Last edited: Mar 7, 2015