Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 chrisWhyTea · Apr 06, 2017 at 07:26 PM · 2dposition

position of 2d sprites is way off

Hello I'm pretty new to unity and i have a problem with the placement of 2d sprites... Im trying to create a isometric grid and it kinda works, but the gaps between the cells is way to big. Here the code i use:

 void CreateGrid()
     {
         grid = new GridCell[(int)size.x,(int)size.y];
         for (int ix = 0; ix < (int)size.x; ++ix)
         {
             for (int iy = 0; iy < (int)size.y; ++iy)
             {
                 float cell_x = ((ix - iy) * (GridCell.CELL_WIDTH / 2)); // CELL_WIDTH = 128
                 float cell_y = ((ix + iy) * (GridCell.CELL_HEIGHT / 2)); // CELL_HEIGHT = 64
                 float cell_z = 0;
                 Vector3 position = new Vector3(cell_x, cell_y, cell_z);
                 GridCell cell = Instantiate(gridCell, position, Quaternion.identity);
                 grid[ix, iy] = cell;
             }
         }
     }

My Goal is to get a grid without gaps, that still could be used in world space....

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 Rhombuster · Apr 06, 2017 at 09:50 PM

I didn't quite understand what you were doing, but here is a simpler way:

 void CreateGrid(float start_x, float start_y, float start_z) {
     grid = new GridCell[(int)size.x, (int)size.y];
     float current_cell_x = start_x;
     float current_cell_y = start_y;
     float current_cell_z = start_z; // Just in case you want to change it later

     for(int y = 0; y < (int)size.y, y++) {
         current_cell_x = start_x;
         for(int x = 0; x < (int)size.x, x++) {
             Vector3 position = new Vector3(current_cell_x, current_cell_y, current_cell_z);
             GridCell cell = Instantiate(gridCell, position, Quaternion.identity);
             grid[y, x] = cell;
             current_cell_x += GridCell.CELL_WIDTH;
         }
         current_cell_y += GridCell.CELL_HEIGHT;
     }
 }

Just give it a starting x, y and z and it should tile it properly. This starts tiling from left to right, then bottom to top. If you'd like to switch directions, say going top down or right to left, you can just change the += operator to -=. I didn't test this, so let me know if it doesn't work.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I check if/where in a sprite another sprite is? 0 Answers

Setting children position to Vector3.zero doesn't make them go to center of parent? 1 Answer

Mouse position not returning actual position 0 Answers

Spawn objects at random position ON a 2D object. 1 Answer

Shoot second cell in 2d 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