Creating a grid using an object as the center and forward object.

I’m having a heck of a time wrapping my head around this. I have a script that generates a grid centered on an object sized based on XDistance, ZDistance and spacing. (These are all converted to feet).

It generates points from the top left to the bottom right (currently in world space). This all works great. But I want it to generate those points relative to the origin’s transform.forward rather than global forward. I can’t seem to figure out how to translate each point or generate each point with consideration to the origins forward.

Here is what I have so far - Again this works fine but only generates based on Global forward rather than local forward.

    public Grid GenerateNewGrid(Transform origin)
    {

        Debug.Log(origin.forward);
        Debug.Log(origin.forward.normalized);
        float zFT = ZDistance * spacing;
        float xFT = XDistance * spacing;

        Grid myGrid = new Grid();
        myGrid.myRows = new GridX[ZDistance];
        for(int f = 0; f < myGrid.myRows.Length; f++)
        {
            GridX x = myGrid.myRows[f] = new GridX();
            x.point = new Vector3[XDistance];
        }

        GameObject go = new GameObject("temp");
        go.transform.position = origin.position;
        go.transform.rotation = origin.rotation;

        //GameObject child = new GameObject("child");

        //child.transform.SetParent(go.transform);

        for (int y = 0; y < ZDistance; y++)
        {
            for(int x = 0; x < XDistance; x++)
            {
                //Add a vector3 point starting at the top left and moving right and down in 1ft increments
                Vector3 newP = new Vector3( ( origin.position.x + (spacing*x)), 0 , (origin.position.z + (-y * spacing)));

                Vector3 newPos = new Vector3( (newP.x - (xFT / 2)) + (spacing/2), newP.y,  (newP.z + (zFT/2)) - (spacing / 2));

                myGrid.myRows[y].point[x] = newPos;
            }
        }
        return myGrid;
    }

Anyone a vector wizard?

Thanks,
Craig

No sir, but I have multiplied by a Quaternion or two, and Vectors hold no terrors for me.

Rather than me try to understand your code above, let me offer you this insight.

If you have a Transform, that transform has some useful Vector3 shortcuts that you can use to do your spacing. These are unit vectors that go in the local +X, +Y and +Z space of that Transform.

They are called transform.right, transform.up, and transform.forward.

If when you are iterating out your grid you use the:

  • iteration integers
  • those Vectors
  • the axis spacing

… you will end up where you want.

If x and z are your iteration integers, and it looks like your spacing is uniform on both, the positions will be:

Vector3 pos = origin + transform.right * (x * spacing) + transform.forward * (z * spacing);

(I converted your y iterator to z for clarity).

I figured it out and the answer was extremely obvious. I think I just needed to step back and rethink it.

for (int y = 0; y < ZDistance; y++)
        {
            for(int x = 0; x < XDistance; x++)
            {
                //Add a vector3 point starting at the top left and moving right and down in 1ft increments
                Vector3 newP = new Vector3( ( origin.position.x +  (spacing*x)), 0 , (origin.position.z + (-y * spacing)));

                Vector3 newPos = new Vector3( (newP.x - (xFT / 2)) + (spacing/2), newP.y,  (newP.z + (zFT/2)) - (spacing / 2));

                Vector3 dir = newPos - origin.position;

                dir.y = 0;

                newPos = origin.position + (origin.right * dir.x) + (origin.forward * dir.z);

                newPos.y = 0;

                myGrid.myRows[y].point[x] = newPos;
            }
        }
        return myGrid;

The line newPos = origin.position + (origin.right * dir.x) + (origin.forward * dir.z); was what I needed.

Dir is just the direction from the origin to the newPos that was calculated along the global Z axis. If its multiplied by the origin’s right and forward we get the same distance and angle from the origins forward that the point was from the global Z.