Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by TacoMakerMan · Dec 24, 2017 at 08:27 AM · positiongridvector2snapsnapping

Snap object to grid with offset?

I'm trying to make sure that an object snaps to a grid, but I want it to be in the center of the grid squares instead of the intersection between the grid lines.

Here's a method that returns a position but snapped to a grid size of 2 so for instance the x values that could be returned are -4, -2, 0, 2, 4, 6...

 Vector2 SnapToGrid (Vector2 pos)
 {
     Vector2 gridPos = pos;
     gridPos.x = Mathf.Round (pos.x / gridSize.x) * gridSize.x;
     gridPos.y = Mathf.Round (pos.y / gridSize.y) * gridSize.y;
 
     return gridPos;
 }

but I need a method that would instead return x values of -5, -3, -1, 1, 3, 5... so on an offset.

Here's an image to help show what I mean: alt text

helpmeplease.png (42.9 kB)
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
1

Answer by Nikaas · Dec 24, 2017 at 08:46 AM

You can just add/subtract half a "tile" after you do the rounding that calculates the grid position.

  Vector2 SnapToGrid (Vector2 pos)
  {
      Vector2 gridPos = pos;
      gridPos.x = Mathf.Round (pos.x / gridSize.x) * gridSize.x + gridSize.x / 2f;
      gridPos.y = Mathf.Round (pos.y / gridSize.y) * gridSize.y + gridSize.y / 2f;
  
      return gridPos;
  }
Comment
Add comment · Show 2 · 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 TacoMakerMan · Dec 24, 2017 at 12:17 PM 0
Share

I have tried this and it doesn't work, since the grid is in the middle, if you just add half a tile then when the position is on the right side then it goes too far and ends up outside of the grid.

It's a good suggestion and might work in some circumstances but not in this case.

avatar image Nikaas · Dec 24, 2017 at 12:44 PM 0
Share

Then substract ins$$anonymous$$d. You still may end up needing to handle edge cases because even on you images one grid is with more positions than the other. You would have to choose if you want to ignore the first or last x/y (or correct the grid dimensions by one).

avatar image
0

Answer by dracozinc · Jan 30 at 02:04 AM

 Vector2 SnapToGrid (Vector2 pos)
     {
         int offset = -1;
         Vector2 gridPos = pos;
         gridPos.x = Mathf.Round ((pos.x - offset) / gridSize.x) * gridSize.x + offset;
         gridPos.y = Mathf.Round ((pos.y - offset) / gridSize.y) * gridSize.y + offset;
         
         return gridPos;
     }

here is another solution

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

86 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

Related Questions

Grid snapping 0 Answers

Gizmo won't stop snapping to grid. 1 Answer

RTS building snap to grid 2 Answers

Snap All Axes Hotkey? 0 Answers

cannot get snap to grid to activate 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