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 Niki.j · Dec 26, 2012 at 08:29 AM · gridgrid based game

How to create a grid in unity3d?

Hello all,

i am a unity game developer,and i m pretty bad with the grids. i have to create a prototype for cube movement in a grid. but i don't know how to create a grid. any thing like pseudo code or algorithm for creating a grid in unity will be very much helpful for me.

Thanks in Advance! Niki.j

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

3 Replies

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

Answer by save · Dec 26, 2012 at 08:48 AM

Hi Niki!

I got help with this once, the secret is to round off the number.

This script example moves a transform at a predefined direction each second within a grid:

 var gridSize : Vector3 = new Vector3(1,1,1);
 var movementDirection : Vector3 = new Vector3(0,0,1);
 
 function Start () {
     InvokeRepeating("UpdatePosition", 1.0, 1.0);
 }
 
 function UpdatePosition () {
     var newPos : Vector3 = transform.position+movementDirection;
     newPos = Vector3(Mathf.Round(newPos.x/gridSize.x)*gridSize.x,
                      Mathf.Round(newPos.y/gridSize.y)*gridSize.y,
                      Mathf.Round(newPos.z/gridSize.z)*gridSize.z);
     transform.position = newPos;
 }


Comment
Add comment · Show 5 · 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 madmike6537 · Feb 28, 2013 at 03:40 AM 0
Share

Great script. Helped me a lot with my grid project, thanks!

avatar image 8l33d · Sep 30, 2013 at 09:22 PM 0
Share

Hey ! i dont understand where i have to put that script so when i make a cube or add a prefab i can use the grid to place it or scale/rotate it... also id like to know if i could turn the grid on/off. Thanks

avatar image madmike6537 · Sep 30, 2013 at 09:24 PM 0
Share

Well you could just put it in a script called grid and attach is to your prefab. Then you could use a bool which controls enabled = true; to turn it on or off.

avatar image 8l33d · Oct 01, 2013 at 02:56 PM 0
Share

Thanks $$anonymous$$ike! Im trying to make some kind of prefab modules to build levels or maps for a game, so you have different corridors, rooms props etc and you can use the modules to build them however they want. Ill try this and see how it works out!

avatar image lonevetad · Oct 09, 2017 at 09:36 AM 0
Share

The code is correct, but can be easily tuned and simplified: divide, round and multiply could be very slow due to the floating point arithmetic. Insead, use this more intuitive C# script:

 int snap( int value, int size ){
      int remaining;
      int halfSize; //can be cached somewhere
      halfSize = size / 2; // bitwise operation for integer: size >>1;
      remaining = (value % size); // where % is the division's remaining
      if( remaining < halfSize)
          return value - remaining;
      else
          return (value - remaining) +1;
 }
 .....
 // remember to cache the gridSize's half values to optimize
 newPos = Vector3( snap(newPos.x, gridSize.x),
     snap(newPos.y, gridSize.y),
     snap(newPos.z, gridSize.z),
 );

I assumed that newPos.xyz are integer, but could be a float or a double. Just overload the snap method.

avatar image
0

Answer by darkcookie · Dec 26, 2012 at 08:34 AM

http://www.youtube.com/watch?v=810UVUWGlWw

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 USPowerSquadron · Jun 12, 2014 at 07:40 PM 0
Share

I have a related question. How can you check the state of a specific grid space? I know that is a broad question, so here are a few examples: find a game object in a selected grid. maybe there is some type of terrain in that space that affects any game object occupying this space.

avatar image
0

Answer by z25assassins · Apr 03, 2017 at 06:51 AM

try this video https://www.youtube.com/watch?v=Q0nbfq-f1os

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

15 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Create a grid on a cube? 0 Answers

How to find sprite at grid location? 1 Answer

Grid-based building system not working 1 Answer

grid-based block placement system with touch input 1 Answer

2D Board game grid placement 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