• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SAOTA · Feb 18, 2015 at 10:49 AM · childgridperspectiveoverlaygrids

Generate a grid at a specific position

I found an awesome grid generating script by em3rgency

It goes a little something like this:

using UnityEngine; using System.Collections;

public class gridOverlay : MonoBehaviour {

  public GameObject plane;
  
  public bool showMain = true;
  public bool showSub = false;
  
  public int gridSizeX;
  public int gridSizeY;
  public int gridSizeZ;
  
  public float smallStep;
  public float largeStep;
  
  public float startX;
  public float startY;
  public float startZ;
  
  private float offsetY = 0;
  private float scrollRate = 0.1f;
  private float lastScroll = 0f;
  
  private Material lineMaterial;
  
  private Color mainColor = new Color(0f,1f,0f,1f);
  private Color subColor = new Color(0f,0.5f,0f,1f);
  
  void Start () 
  {
 
  }
  
  void Update () 
  {
      if(lastScroll + scrollRate < Time.time)
      {
          if(Input.GetKey(KeyCode.KeypadPlus)) 
          {
              plane.transform.position = new Vector3(plane.transform.position.x, plane.transform.position.y + smallStep, plane.transform.position.z);
              offsetY += smallStep;
              lastScroll = Time.time;
          }
          if(Input.GetKey(KeyCode.KeypadMinus))
          {
              plane.transform.position = new Vector3(plane.transform.position.x, plane.transform.position.y - smallStep, plane.transform.position.z);
              offsetY -= smallStep;
              lastScroll = Time.time;
          }
      }
  }
  
  void CreateLineMaterial() 
  {
 
      if( !lineMaterial ) {
          lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" +
              "SubShader { Pass { " +
              "    Blend SrcAlpha OneMinusSrcAlpha " +
              "    ZWrite Off Cull Off Fog { Mode Off } " +
              "    BindChannels {" +
              "      Bind \"vertex\", vertex Bind \"color\", color }" +
              "} } }" );
          lineMaterial.hideFlags = HideFlags.HideAndDontSave;
          lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;}
  }
  
  void OnPostRender() 
  {        
      CreateLineMaterial();
      // set the current material
      lineMaterial.SetPass( 0 );
      
      GL.Begin( GL.LINES );
      
      if(showSub)
      {
          GL.Color(subColor);
          
          //Layers
          for(float j = 0; j <= gridSizeY; j += smallStep)
          {
              //X axis lines
              for(float i = 0; i <= gridSizeZ; i += smallStep)
              {
                  GL.Vertex3( startX, j + offsetY, startZ + i);
                  GL.Vertex3( gridSizeX, j + offsetY, startZ + i);
              }
              
              //Z axis lines
              for(float i = 0; i <= gridSizeX; i += smallStep)
              {
                  GL.Vertex3( startX + i, j + offsetY, startZ);
                  GL.Vertex3( startX + i, j + offsetY, gridSizeZ);
              }
          }
          
          //Y axis lines
          for(float i = 0; i <= gridSizeZ; i += smallStep)
          {
              for(float k = 0; k <= gridSizeX; k += smallStep)
              {
                  GL.Vertex3( startX + k, startY + offsetY, startZ + i);
                  GL.Vertex3( startX + k, gridSizeY + offsetY, startZ + i);
              }
          }
      }
      
      if(showMain)
      {
          GL.Color(mainColor);
          
          //Layers
          for(float j = 0; j <= gridSizeY; j += largeStep)
          {
              //X axis lines
              for(float i = 0; i <= gridSizeZ; i += largeStep)
              {
                  GL.Vertex3( startX, j + offsetY, startZ + i);
                  GL.Vertex3( gridSizeX, j + offsetY, startZ + i);
              }
              
              //Z axis lines
              for(float i = 0; i <= gridSizeX; i += largeStep)
              {
                  GL.Vertex3( startX + i, j + offsetY, startZ);
                  GL.Vertex3( startX + i, j + offsetY, gridSizeZ);
              }
          }
          
          //Y axis lines
          for(float i = 0; i <= gridSizeZ; i += largeStep)
          {
              for(float k = 0; k <= gridSizeX; k += largeStep)
              {
                  GL.Vertex3( startX + k, startY + offsetY, startZ + i);
                  GL.Vertex3( startX + k, gridSizeY + offsetY, startZ + i);
              }
          }
      }
 
 
      GL.End();
  }

}

I'd like to know If I can specify where this grid is generated. On an empty game object perhaps.

I would like this grid to move with the camera. So I would like to generate the grid in a position specified by a child gameobject of the camera.

So when the camera moves the grid moves relative to the camera.

Any Ideas?

Any help would be greatly appreciated.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Graham-Dunnett · Feb 18, 2015 at 10:53 AM

Every line that has GL.Vertex3 specifies the x,y,z location of a point in space. If you want these points to follow some position you control, just add the offset to each x,y,z component:

 // SOATAOffset is a Vector3 that is computed from the camera position
 // line 133
 GL.Vertex3( SOATAOffset.x + startX + k, SOATAOffset.y + gridSizeY + offsetY, SOATAOffset.z + startZ + i);


You'll need to make this change everywhere, not just line 133.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to detect the border of a canvas grid and fill the slots of the grid with different shapes of blocks? 2 Answers

Limiting size of a tilemap grid and populating it through a file 0 Answers

Grid and or Tile System 0 Answers

Make a simple tree 1 Answer

orthographic and perspective camera overlay 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges