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 youngapprentice · Jan 04, 2013 at 10:50 PM · javascripteditorgrid

Grid Creation from Center

EDIT: Here is the finished script. It takes variables for Width, Height, Rows, and Columns. If you attach this to an empty, it will use the empty as a center point and construct the grid

 #pragma strict
 @script ExecuteInEditMode
 var debug : boolean;
 
 var columns : int;
 var rows : int;
 
 var gridWidth : int;
 var gridHeight : int;
 
 @HideInInspector
 var deltaWidth : float;
 @HideInInspector
 var deltaHeight : float;
 @HideInInspector
 var gridArray : Vector3[,];
 
 function Awake () {
 
   //Does the math to find the relative distance between points (Index-Based means we have to subtract 1 from column/row count)
  deltaWidth = ( gridWidth / ( columns-1 ));
  deltaHeight = ( gridHeight / ( rows-1 ));
 
   gridArray = new Vector3[ rows , columns ];
   
   for( var u = 0; u < rows; u++ ){
   
       for( var i = 0; i < columns; i++ ){
           
           //Constructing grid using the transform as the UPPER LEFT CORNER
           gridArray[u,i] = new Vector3( transform.position.x + (deltaWidth*i), transform.position.y + ( -deltaHeight*u ), transform.position.z );
           //These next two lines offset the points so that the transform is now used as the CENTER --(remove these if you want to use it as the corner)--
           gridArray[u,i].x = gridArray[u,i].x - ( gridWidth/2);
           gridArray[u,i].y = gridArray[u,i].y + ( gridHeight/2);          
   }}}
   //Used to draw the grid in Editor
   function DrawGrid(){
       
       for( var q = 0; q < (columns); q++){
           
           var column_top : Vector3 = gridArray[q,0];
         var column_bottom : Vector3 = gridArray[q, (rows-1) ];
         Debug.DrawLine (column_top, column_bottom, Color.red);
           
       }
       
       for( var w = 0; w < (rows); w++){
           
           var row_top : Vector3 = gridArray[0,w];
           var row_bottom : Vector3 = gridArray[ (columns-1), w ];
           Debug.DrawLine ( row_top, row_bottom, Color.red);
           
       }}
   //If debug is active, draw the grid
   function Update(){
       if(debug)    DrawGrid();    
   }

Hi, all! I am using a grid as a method of picking a spot to spawn my enemies.

I have some problems though:

  1. I have created a script that creates a grid from the transform.position of the empty it is attached to being the Upper Left corner, but I would like to know how to construct the grid from the empty it is attached to acting as a center point.

  2. I have no idea how I would use Debug.DrawLine to visualize the grid. Some help with that would be lovely. I only need it to be visualized in the editor, as it will not be visible to the player.

  3. In my script that creates the grid by using the empty as the Upper Left Corner, the width and height variables seem to be off. For some strange reason, I have to set them much higher than they should be to get what I want. Is there a problem with my math (if we can come up with a solution for a center-based grid, then this problem does not have to be addressed)

A note: I am using UnityScript

Hope this isn't too much trouble.

Thanks!- YA

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by jogo13 · Jan 05, 2013 at 12:48 AM

To center it you need to offset the grid by minus half it's height and minus half it's width.

For problem 1: You could keep the grid creation code as you have it and just create a for loop afterward that goes through each point on the grid and offsets:

 for(int i, i < gridpoints.Length, i++)
 {
    gridpoints[i].x = gridpoints[i].x - (grid_width/2);
    gridpoints[i].y = gridpoints[i].y - (grid_height/2);
 }

For problem 2: I would create a seperate draw_grid function and place it in your Update function as it will need to be called every frame. You can also place it in a function called OnDrawGizmos but that will play even if the grid hasn't been drawn. Maybe something like:

     //Drawing columns (untested)
     int point = 0;
         for(int i, i < column_count, i++)
             {
                
                Vector3 column_top = gridpoints[i*column_count].position;
                Vector3 column_bottom = gridpoints[i*column_count*row_count].position;
                Debug.DrawLine (gridpoints[x].position, gridpoints[x].position, Color.red);
              
     
             }
 
 //Draw rows in a similar fashion

For problem 3: it could be your confusing transform.position with transform.localposition

Comment
Add comment · Show 1 · 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
avatar image youngapprentice · Jan 05, 2013 at 01:41 AM 0
Share

Well I simply rewrote a couple things and it works like a charm now. Thank you very much, Jogo! I will mark your answer as the answer and will post the finished script in my question.

avatar image
1

Answer by Davidovich · Jan 05, 2013 at 01:34 AM

Firstly, I'd recommend posting some of the scripts you mention. It's much easier to see what you're trying to do and much easier to offer advice on :)

In terms of the three questions above:

Question 1

What I assume you mean by a "center-based grid" is that you want to interact with the cells, rather than the points of the grid. For instance, if you say "Put this in grid 1,1 then it appears in the centre of the top left cell, not where the first two lines cross.

When it comes down to it, a grid is actually a set of coordinates at regular intervals across the space. What we generally represent are the points or lines of the grid, not the cells. This means is that if you want to do something with the cell instead of the line you need to offset it from the point on the grid.

There are a bunch of ways of doing this, which may or may not work depending on what your grid is actually for.

  • Simply move the empty gameObject over by half the width of the cell. (EG. if each grid cell is 1x1, move your gameobject to 0.5, 0.5).

  • When you are calculating your grid, start with an offset of half the cell width.

  • When drawing the grid lines, or placing objects add a half-cell offset to their transforms.

  • Create a new data structure to represent the cells. So instead of a grid of points you have a grid of cells and each one can easily calculate its centre, edges, etc.

If you can add a bit more detail as to what you want to do with the grid I'm happy to advise on the best way to sort this out.

Question 2

To draw the grid, you want to iterate over each of the coordinates on the sides of your grid and draw the line across. How exactly you do this will depend on how you're representing the points of the grid.

Basically, in completely un-tested code:

 var gridSize = 5f;
 for (var i = 0; i < gridSize ; i++) {
     Debug.DrawLine(Vector3(i, 0, 0), Vector3(i, gridSize, 0));
     Debug.DrawLine(Vector3(0, i, 0), Vector3(gridSize, i, 0));
 }

This is greatly simplified, obviously.

It also assumes that you have a finite grid. If you're wanting it unbounded you'll have to figure out what portion of it you're drawing at the moment, as you can't just draw forever :)

Question 3

There may well be a problem with your maths. If you put up the scripts well be able to tell you. :-)

It could also be to do with the scaling of the empty gameObject.

Comment
Add comment · Show 3 · 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
avatar image Davidovich · Jan 05, 2013 at 01:35 AM 0
Share

I also just realised that I'm only talking about 2D grids above. All the same things apply to a 3D grid, they're just a bit more complex :-)

avatar image youngapprentice · Jan 05, 2013 at 01:40 AM 0
Share

When I mentioned 'center-based grid', I actually did indeed mean 'The grid should be constructed around a given center point ins$$anonymous$$d of any other point'.

Your explanation was very in-depth, and all though I have all ready solved the script and have it completed now, I will upvote your answer because I admire how neat and explanatory it is. Thank you very much for your time! :)

avatar image Davidovich · Jan 05, 2013 at 01:42 AM 0
Share

Haha! Thanks for that :) Glad you got it working!

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GRID missing - Editor Scene View - 5.0.1f1 - not a duplicate 0 Answers

UnityScript equivalent to C# Action 1 Answer

How can I execute code in the editor? 1 Answer

Monodevelop autocomplete on private variables 1 Answer

Editing Script In Code? 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