Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

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

Grid snapping 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges