Search Unity

Trying to generate one row(25 blocks long(explanding downwards later))

Discussion in '2D' started by jaden03, Aug 11, 2019.

  1. jaden03

    jaden03

    Joined:
    Nov 24, 2018
    Posts:
    9
    I am making a game inspired by a phone game called Mars Miner(played it like 7 years ago), im trying to make the ores and stuff procedurally generated, they generate randomly, but I cant seem to get them to stop spawning on top of eachother. Also my block grid is 1.05 x and 0.96 y(that's what the round and round2 functions are), anyways here is my code, which is on the player for generation.

    Code (CSharp):
    1.     private float xgrid = 1.05f;
    2.     private float ygrid = 0.96f;
    3.     private Vector2 topleft;
    4.     private Vector2 topright;
    5.     private Vector2 position;
    6.     List<Vector2> randomNumber = new List<Vector2>();
    7.     public GameObject Stone;
    8.     public GameObject Coal;
    9.     private GameObject Block;
    10.  
    11.     private float Round(float input)
    12.     {
    13.         return xgrid * Mathf.Round((input / xgrid));
    14.     }
    15.  
    16.     private float Round2(float input)
    17.     {
    18.         return ygrid * Mathf.Round((input / ygrid));
    19.     }
    20.  
    21.     // Use this for initialization
    22.     void Start()
    23.     {
    24.         topleft = new Vector2(-12.6f, -2.88f);
    25.         topright = new Vector2(12.6f, -2.88f);
    26.         Generate();
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Generate()
    31.     {
    32.         for (int i = 0; i < 25; i++)
    33.         {
    34.             {
    35.                 if (Random.value < 0.05)
    36.                 {
    37.                     Block = Coal;
    38.                 }
    39.                 else
    40.                 {
    41.                     Block = Stone;
    42.                 }
    43.                 Vector2 position = new Vector2(Round(Random.Range(-12.6f, 12.6f)), (Round2(Random.Range(-2.88f, -2.88f))));
    44.                 randomNumber.Add(position);
    45.                 print(position);
    46.                 while (randomNumber.Contains(position))
    47.                 {
    48.                     randomNumber.Remove(position);
    49.                     position = new Vector2(Round(Random.Range(-12.6f, 12.6f)), (Round2(Random.Range(-2.88f, -2.88f))));
    50.                 }
    51.                 Instantiate(Block, position, Quaternion.identity);
    52.             }
    53.         }
    54.     }
    55. }
    56.  
     
    Last edited: Aug 11, 2019
  2. czzplnm

    czzplnm

    Joined:
    Jul 16, 2019
    Posts:
    41
    You're generating a random X / Y position each iteration. You need to take the height / width of the Block and offset each iteration further more to the right and when creating a new row, use the height as an offset.
     
    jaden03 likes this.
  3. jaden03

    jaden03

    Joined:
    Nov 24, 2018
    Posts:
    9
    Your right, I dont need to generate anything, and now I feel dumb, because I was trying to fix this all day
    Im going to attempt doing that
     
  4. jaden03

    jaden03

    Joined:
    Nov 24, 2018
    Posts:
    9
    So I got this to fill in the row, but how would I expand downwards?
    Code (CSharp):
    1.     private float xgrid = 1.05f;
    2.     private float ygrid = 0.96f;
    3.     private Vector2 topleft;
    4.     private Vector2 topright;
    5.     private Vector2 position;
    6.     List<Vector2> randomNumber = new List<Vector2>();
    7.     public GameObject Stone;
    8.     public GameObject Coal;
    9.     private GameObject Block;
    10.  
    11.     private float Round(float input)
    12.     {
    13.         return xgrid * Mathf.Round((input / xgrid));
    14.     }
    15.  
    16.     private float Round2(float input)
    17.     {
    18.         return ygrid * Mathf.Round((input / ygrid));
    19.     }
    20.  
    21.     // Use this for initialization
    22.     void Start()
    23.     {
    24.         topleft = new Vector2(-12.6f, -2.88f);
    25.         topright = new Vector2(12.6f, -2.88f);
    26.         position = topleft;
    27.         Generate();
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Generate()
    32.     {
    33.         for (int i = 0; i < 25; i++)
    34.         {
    35.             {
    36.                 if (Random.value < 0.05)
    37.                 {
    38.                     Block = Coal;
    39.                 }
    40.                 else
    41.                 {
    42.                     Block = Stone;
    43.                 }
    44.                 Instantiate(Block, position, Quaternion.identity);
    45.                 position = position + new Vector2(1.05f, 0f);
    46.             }
    47.         }
    48.     }
    49. }
    50.  
     
  5. jaden03

    jaden03

    Joined:
    Nov 24, 2018
    Posts:
    9
    I have managed to expand downwards, but it takes a few lines per expansion downward, any ideas to shorten this?

    Code (CSharp):
    1.     void Generate()
    2.     {
    3.         for (int i = 0; i < 50; i++)
    4.         {
    5.             {
    6.                 if (Random.value < 0.05)
    7.                 {
    8.                     Block = Coal;
    9.                 }
    10.                 else
    11.                 {
    12.                     Block = Stone;
    13.                 }
    14.                 if (i < 25 && i != 0)
    15.                 {
    16.                     position = position + new Vector2(1.05f, 0f);
    17.                 }
    18.                 else if (i == 25)
    19.                 {
    20.                     position = topleft + new Vector2(0f, -0.96f);
    21.                 }
    22.                 else if (i > 25)
    23.                 {
    24.                     position = position + new Vector2(1.05f, 0f);
    25.                 }
    26.                 Instantiate(Block, position, Quaternion.identity);
    27.             }
    28.         }
    29.     }
    30. }
    31.  
    And that's just 1 line under the top one
     
  6. czzplnm

    czzplnm

    Joined:
    Jul 16, 2019
    Posts:
    41
    Two FOR loops one for the number of rows, one for the number of columns.

    Code (CSharp):
    1. blockHeight * row = yPos
    2. blockWidth * column = xPos
     
  7. jaden03

    jaden03

    Joined:
    Nov 24, 2018
    Posts:
    9
    How would I incorporate that into my script? Sorry im just starting to code again so im still getting back into it
     
  8. czzplnm

    czzplnm

    Joined:
    Jul 16, 2019
    Posts:
    41
    Something like this I used for another game:

    Code (CSharp):
    1.  foreach (string row in level)
    2.         {
    3.             if (rowNum >= maxObjectsRows)
    4.             {
    5.                 break;
    6.             }
    7.             columnNum = 0;
    8.             string[] columns = row.Split(',');
    9.             foreach (string column in columns)
    10.             {
    11.                 //blank
    12.                 if (column == "")
    13.                 {
    14.                     columnNum++;
    15.                     continue;
    16.                 }
    17.  
    18.                 string[] details = column.Split(':');
    19.                 var count = column;
    20.                 double xMod = columnNum % maxObjectsColumn;
    21.                 double yMod = rowNum % maxObjectsRows;
    22.  
    23.                 float xPos = (float)((widthPerBlock * xMod) + (originX + xOffset));
    24.                 float yPos = (float)((heightPerBlock * yMod) + (originY + yOffset));
    25.  
    26.            
    27.  
    28.                 bool isScaleFlipped = false;
    29.                 if (details.Length == 2)
    30.                 {
    31.                     var type = details[0];
    32.                     count = details[1];
    33.                     switch (type)
    34.                     {
    35.                         case "ttr":
    36.                             block = Instantiate(gameTriangle);
    37.                             blockRT = block.GetComponent<RectTransform>();
    38.                             blockRT.Rotate(new Vector3(0, 0, 180));
    39.                             //blockRT.rotation = new Quaternion(0, 0, 180, 0);
    40.                             textRT = block.GetComponentInChildren<Text>().GetComponent<RectTransform>();
    41.                             //textRT.rotation = new Quaternion(0, 0, -180, 0);
    42.                             textRT.Rotate(new Vector3(0, 0, -180));
    43.                             polyCol2d = block.GetComponent<PolygonCollider2D>();
    44.                             break;
    45.                         case "ttl":
    46.                             block = Instantiate(gameTriangle);
    47.                             blockRT = block.GetComponent<RectTransform>();
    48.                             //blockRT.rotation = new Quaternion(0, 0, 270, 0);
    49.                             blockRT.Rotate(new Vector3(0, 0, 270));
    50.                             isScaleFlipped = true;
    51.                             textRT = block.GetComponentInChildren<Text>().GetComponent<RectTransform>();
    52.                             textRT.Rotate(new Vector3(0, 0, -270));
    53.                             polyCol2d = block.GetComponent<PolygonCollider2D>();
    54.                             break;
    55.                         case "tbr":
    56.                             block = Instantiate(gameTriangle);
    57.                             blockRT = block.GetComponent<RectTransform>();
    58.                             //blockRT.rotation = new Quaternion(0, 0, 90, 0);
    59.                             blockRT.Rotate(new Vector3(0, 0, 90));
    60.                             isScaleFlipped = true;
    61.                             textRT = block.GetComponentInChildren<Text>().GetComponent<RectTransform>();
    62.                             //textRT.rotation = new Quaternion(0, 0, -90, 0);
    63.                             textRT.Rotate(new Vector3(0, 0, -90));
    64.                             polyCol2d = block.GetComponent<PolygonCollider2D>();
    65.                             break;
    66.                         default:
    67.                         case "tbl":
    68.                             //default
    69.                             block = Instantiate(gameTriangle);
    70.                             textRT = block.GetComponentInChildren<Text>().GetComponent<RectTransform>();
    71.                             blockRT = block.GetComponent<RectTransform>();
    72.                             polyCol2d = block.GetComponent<PolygonCollider2D>();
    73.                             break;
    74.  
    75.                     }
    76.                 }
    77.                 else
    78.                 {
    79.                     block = Instantiate(gameBlock);
    80.                     blockRT = block.GetComponent<RectTransform>();
    81.                     textRT = block.GetComponentInChildren<Text>().GetComponent<RectTransform>();
    82.                     blockCol2d = blockRT.GetComponent<BoxCollider2D>();
    83.                 }
    84.  
    85.                 block.SendMessage("setBlockCount", count);
    86.                 block.transform.SetParent(gamePanelRT, false);
    87.                 //scale
    88.                 var xScale = (float)(widthPerBlock / 50);
    89.                 var yScale = (float)(heightPerBlock / 50);
    90.                 if (isScaleFlipped)
    91.                 {
    92.                     blockRT.localScale = new Vector3(yScale, xScale, 0);
    93.                 }
    94.                 else
    95.                 {
    96.                     blockRT.localScale = new Vector3(xScale, yScale, 0);
    97.                 }
    98.  
    99.                 blockRT.transform.localPosition = new Vector3(xPos, yPos);
    100.                 columnNum++;
    101.             }
    102.             rowNum++;
    103.         }
     
  9. jaden03

    jaden03

    Joined:
    Nov 24, 2018
    Posts:
    9
    Im so confused, I cant find anything that will help me in this code.
     
  10. czzplnm

    czzplnm

    Joined:
    Jul 16, 2019
    Posts:
    41
    From your code you are trying to make 25 rows and 25 columns of Blocks right? Thats what the code I post would do.
     
  11. jaden03

    jaden03

    Joined:
    Nov 24, 2018
    Posts:
    9
    Thanks! But I have finally achieved it, I wanted to have a customizable deepness. Here is the code I used incase your curious! Thanks!
    Code (CSharp):
    1.     private float xgrid = 1.05f;
    2.     private float ygrid = 0.96f;
    3.     private Vector2 topleft;
    4.     private Vector2 topright;
    5.     private Vector2 position;
    6.     List<Vector2> randomNumber = new List<Vector2>();
    7.     public GameObject Stone;
    8.     public GameObject Coal;
    9.     private GameObject Block;
    10.     private float row;
    11.     private float lastrow = 0;
    12.  
    13.     private float Round(float input)
    14.     {
    15.         return xgrid * Mathf.Round((input / xgrid));
    16.     }
    17.  
    18.     private float Round2(float input)
    19.     {
    20.         return ygrid * Mathf.Round((input / ygrid));
    21.     }
    22.  
    23.     // Use this for initialization
    24.     void Start()
    25.     {
    26.         topleft = new Vector2(-12.6f, -2.88f);
    27.         topright = new Vector2(12.6f, -2.88f);
    28.         Generate();
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Generate()
    33.     {
    34.         for (int i = 0; i < 200; i++)
    35.         {
    36.             {
    37.                 row = i / 25;
    38.                 if (Random.value < 0.05)
    39.                 {
    40.                     Block = Coal;
    41.                 }
    42.                 else
    43.                 {
    44.                     Block = Stone;
    45.                 }
    46.                 if (i == 0)
    47.                 {
    48.                     position = topleft;
    49.                 }
    50.                 else if (lastrow != row)
    51.                 {
    52.                     lastrow = row;
    53.                     position = topleft + new Vector2(0f, -0.96f * row);
    54.                 }
    55.                 else if (lastrow == row)
    56.                 {
    57.                     position = position + new Vector2(1.05f, 0f * row);
    58.                 }
    59.  
    60.                 Instantiate(Block, position, Quaternion.identity);
    61.             }
    62.         }
    63.     }
    64. }
    65.