-1

I am trying to start work on learning strategy/base building games in unity. I started out by copying code from this video but my version only does a single line of squares.

It is the z dimension that does not continue and it does seem like it is a problem with the triangles/normals as the vertices seem to be in the right place via making the vertices a public variable and seeing them in unity's inspector. I think I'd have to write a widget to show the vertices to be sure as I'm no mathematician. I removed the actual creation of the object in unity for the sake of brevity as there is no math involved. Also the procedural object does seem to crumble to nothing at larger sizes, although that's not really an issue until the first one is fixed.

void BuildMesh() 
{
    int numTiles = size_x * size_z;
    int numTris = numTiles * 2;

    int vsize_x = size_x + 1;
    int vsize_z = size_z + 1;
    int numVerts = vsize_x * vsize_z;

    //Generate Mesh Data
    vertices = new Vector3[ numVerts ];
    Vector3[] normals = new Vector3[ numVerts ];
    Vector2[] uv = new Vector2[ numVerts ];

    int[] triangles = new int[ numTris * 3];

    int x, z;
    for (z = 0; z < vsize_z; z++) 
    {
        for (x = 0; x < vsize_x; x++) 
        {
            vertices[z * vsize_x + x] = new Vector3(x * tileSize, 0, z * tileSize);
            normals[ z * vsize_x + x] = Vector3.up;
            uv[z * vsize_x + x] = new Vector2((float)x / vsize_x, (float)z / vsize_z);
        }
    }

    for (z = 0; z < size_z; z++) 
    {
        for (x = 0; x < size_x; x++) 
        {
            int squareIndex = x * size_x + x;
            int triOffset = squareIndex * 6;
            triangles[triOffset + 0] = z * vsize_x + x +           0;
            triangles[triOffset + 1] = z * vsize_x + x + vsize_x + 0;
            triangles[triOffset + 2] = z * vsize_x + x + vsize_x + 1;

            triangles[triOffset + 3] = z * vsize_x + x +           0;
            triangles[triOffset + 4] = z * vsize_x + x + vsize_x + 1;
            triangles[triOffset + 5] = z * vsize_x + x +           1;
        }
    }

The code from the video:

this is for the person posting about digging through the video

derHugo
  • 83,094
  • 9
  • 75
  • 115
Lawrence
  • 1
  • 3
  • that last loop looks wonky at first glance, why add vsize_x twice? surely at least one of them is z? in fact the one before it looks strange too but, Im not watching a video to play spot the difference for you – BugFinder Sep 10 '19 at 15:12
  • 1
    `int squareIndex = x * size_x + x;` rather has to be `int squareIndex = z * size_x + x;` .. I'm voting to close this question as off-topic due to: **This question was caused by a problem that can no longer be reproduced or a simple typographical error.** – derHugo Sep 10 '19 at 16:37

1 Answers1

0

derHugo answered the question in the comments. Just posting this to mark it as answered. Thanks.

Lawrence
  • 1
  • 3
  • please don't do this ;) The question will probably be closed soon and until you are able to accept your own (not) "answer" it takes 2 days anyway ;) You could also consider to delete it right away (but shouldn't repeat this that often) – derHugo Sep 10 '19 at 16:41
  • I always feel like using stack overflow is a bit like learning a language all it's own. Thanks for the advice! – Lawrence Sep 10 '19 at 18:15