• 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 /
  • Help Room /
avatar image
0
Question by Alijeen · Oct 23, 2021 at 03:11 PM · gridpositioningalgorithmdynamically

How to get vector3 grid position for object dynamically

Hi, I want to create a simple algorithm for placing cubes with grid positioning. I know it's easy with pre calculated positions and spawn cubes around it with triple loops but what I want to achieve is every time I request position from a function, It should return me the vector position for it dynamically.


For example: row size = 2, and starting point is Vector3.zero.
1. Vector3(0,0,0);
2. Vector3(1,0,0);
3. Vector3(0,0,1);
4. Vector3(1,0,1);
5. Vector3(0,1,0);
6. Vector3(1,1,0);
7. Vector3(0,1,1);
8. Vector3(1,1,1);

It should return values like this. Anyone has an idea about this? Here what I tried so far but it places objects same poisition with some pattern I could not understand.


  public Vector3 GetNextPos()
 {
     Vector3 nextPos;

     if (itemCountInRowX < rowSize)
     {
         nextPos = new Vector3(currentX, currentY, currentZ);
         currentX += cubeSize;
         itemCountInRowX++;
     }
     else if (itemCountInRowX == rowSize && itemCountInRowZ < rowSize)
     {
         currentX = startPos.x;
         currentZ += cubeSize;
         nextPos = new Vector3(currentX, currentY, currentZ);
         if (itemCountInRowZ == 0)
             itemCountInRowZ = 2;
         else
             itemCountInRowZ++;
         itemCountInRowX = 0;
     }
     else if (itemCountInRowX == rowSize && itemCountInRowZ == rowSize)
     {
         itemCountInRowX = 0;
         itemCountInRowZ = 0;
         currentY += cubeSize;
         currentX = startPos.x;
         currentZ = startPos.z;
         nextPos = new Vector3(currentX, currentY, currentZ);
     }
     else
         nextPos = Vector3.zero;

     return nextPos;
 }
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
Best Answer

Answer by TheMemorius · Oct 24, 2021 at 01:48 PM

Does the order of the returned positions have to be exactly like the example you gave (like (0, 0, 1) coming directly after (1, 0, 0) etc.)? If not, this code will create a new cube in a 3d grid each time you click the button:

 using UnityEngine;
 
 class SpawnTest : MonoBehaviour
 {
     public int RowSize = 2;
     public float CubeSize = 1.5f;
 
     private int _count = 0;
 
     public Vector3 GetNextPos()
     {
         int x = _count % RowSize;
         int y = (_count / RowSize) % RowSize;
         int z = (_count / RowSize) / RowSize;
 
         ++_count;
         if(_count >= Mathf.Pow(RowSize, 3))
             _count = 0;
 
         return new Vector3(x, y, z) * CubeSize;
     }
 
     private void OnGUI()
     {
         if(GUI.Button(new Rect(10, 10, 100, 50), "Create next cube"))
         {
             GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
             cube.transform.position = GetNextPos();
         }
     }
 }
 

The magic word here is modulo.

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 Alijeen · Oct 24, 2021 at 02:30 PM 0
Share

Thank you for the answer. Order is important for me but I'll take it from here and try to do it myself. This is very helpful.

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

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

Related Questions

Approach for filling 6x6 matrix with shapes 2 Answers

Positioning Screen Space UI element in center of a grid square? 1 Answer

Snap object to grid made from cubes during runtime 0 Answers

Connecting Pods/Nodes together on a grid using pathfinding? 0 Answers

Generate 3d Mesh from 2d drawing 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