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. Dismiss Notice

Cube Pyramid

Discussion in 'Scripting' started by cowmilk9, Oct 11, 2013.

  1. cowmilk9

    cowmilk9

    Joined:
    Oct 11, 2013
    Posts:
    2
    Hey! I'm pretty new to unity and I need help trying to create a cube pyramid using cube prefabs. So far, I'm able to create two-dimensional stairs using the following code and referencing the cube prefab to an empty game object:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class StepsBuilder : MonoBehaviour
    5. {
    6.     public int height = 0;
    7.     public int width = 0;
    8.     public GameObject brickPrefab = null;
    9.     public int widthCounter = 0;
    10.     public int heightCounter = 0;
    11.     public GameObject awesomeBrick = null;
    12.  
    13.     // Use this for initialization
    14.     void Start ()
    15.     {
    16.         while (heightCounter < height)
    17.         {
    18.             while (widthCounter < width)
    19.             {
    20.                 awesomeBrick = (GameObject)Instantiate(brickPrefab);
    21.                 awesomeBrick.transform.position = new Vector3(widthCounter, heightCounter, 0.0f);
    22.  
    23.                 widthCounter++;
    24.             }
    25.  
    26.             widthCounter = heightCounter + 1;
    27.  
    28.             heightCounter++;
    29.         }
    30.     }
    31.    
    32.     // Update is called once per frame
    33.     void Update ()
    34.     {
    35.    
    36.     }
    37. }
    38.  
    This makes a staircase that, depending on what the user sets the width and height to, looks basically like this (* == cube):

    *
    * *
    * * *
    * * * *
    * * * * *

    Now I need to basically work backwards so the steps go back down to make a two-dimensional pyramid but, where do I go from here? I've tried multiple things, but to no avail. I'm unfortunately completely stuck. Thanks in advance for the help! :)
     
    Last edited: Oct 11, 2013
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You need to offset the start of each row by half a block * row number (minus 1)
     
  3. cowmilk9

    cowmilk9

    Joined:
    Oct 11, 2013
    Posts:
    2
    Would you care to elaborate? Sorry.
     
  4. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    try this. You should really calculate the height from the width as a user might enter a height greater than the width and depending on what the brick prefab is this could cause problems, maybe make the height = width. Anyway this almost works:
    Code (csharp):
    1.     using UnityEngine;
    2.     using System.Collections;
    3.      
    4.     public class PyramidBuilder : MonoBehaviour
    5.     {
    6.         public int height = 0;
    7.         public int width = 0;
    8.         public GameObject brickPrefab = null;
    9.         public GameObject awesomeBrick = null;
    10.         public float xpos=0.0f;
    11.      
    12.         // Use this for initialization
    13.         void Start ()
    14.         {
    15.         for(int i = 0; i < height; i++)
    16.         {
    17.             for(int j = 0; j < width; j++)
    18.             {
    19.                 awesomeBrick = (GameObject)Instantiate(brickPrefab);
    20.                 awesomeBrick.transform.position = new Vector3(j+xpos, i, 0.0f);
    21.             }
    22.             width-=2; // remove 2 bricks from the width
    23.             xpos++; // shift the start position of next row along 1 block can use 0.5 to shift half a block
    24.         }
    25.         }
    26.        
    27.         // Update is called once per frame
    28.         void Update ()
    29.         {
    30.        
    31.         }
    32.     }
     
    Last edited: Oct 11, 2013
  5. msafa160754

    msafa160754

    Joined:
    Jan 24, 2017
    Posts:
    4
    void Pyramid(int pNum)
    {
    for (int y = 0; y < pNum; y++)
    {
    for (int z = 0; z < pNum-y; z++)
    {
    for (int x = 0; x < pNum-y; x++)
    {
    temp = Instantiate(box, new Vector3(x + tNum, y, z + tNum),Quaternion.identity);
    }
    }
    tNum += .5f;
    }

    }