unity3d - how to limit CreateCell c# procedural grid generation unity -
i learning c# unity , use pointers.
i following catlikecoding hex map tutorial have modified grid own means.
http://catlikecoding.com/unity/tutorials/hex-map-1/
my goal create pyramid of squares procedurally starting 7 * 7 grid. using prefab plane
how place limit on createcell
looped function cells (x,y) coordinates not created when meet following expression
x + y > n - 1 n = grid size (for example (6,1) or (5,6)
i have gotten far creating rhombus of planes undesired planes below ground plane.
the script follows.
public class hexgrid : monobehaviour {
public int width = 7; public int height = 7; public int length = 1; public squarecell cellprefab; public text celllabelprefab; squarecell[] cells; canvas gridcanvas; void awake () { gridcanvas = getcomponentinchildren<canvas>(); cells = new squarecell[height * width * length]; (int z = 0 ; z < height; z++) { (int x = 0; x < width; x++) { (int y = 0; y < length; y++) createcell(x, z, y); } } } void createcell(int x, int z, int y) { vector3 position; position.x = x * 10f ; position.y = ((y + 1) - (x + z)) * 10f + 60f; position.z = z * 10f ; cell cell = instantiate<cell>(cellprefab); cell.transform.setparent(transform, false); cell.transform.localposition = position; text label = instantiate<text>(celllabelprefab); label.recttransform.setparent(gridcanvas.transform, false); label.recttransform.anchoredposition = new vector2(position.x, position.z); label.text = x.tostring() + "\n" + z.tostring(); } }
a quick solution add if
statement before part of code creates cell. in case method createcell()
. if statement should have logic in code. have create 2 variables size check. example:
public int tempx; public int tempy; void awake () { gridcanvas = getcomponentinchildren<canvas>(); cells = new squarecell[height * width * length]; (int z = 0 ; z < height; z++) { (int x = 0; x < width; x++) { (int y = 0; y < length; y++) { if (x + y < (tempx + tempy) - 1) { createcell(x, z, y); } } } } }
Comments
Post a Comment