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
2
Question by mormonspam · Jun 14, 2013 at 02:53 AM · 2dgridgrids

Make grid and snap to it in Unity editor (2D Game)

We are building a 2D game like this - I've been using Unity for a while, but have never had to build a grid system like this.

Ideally this would be our pipeline: 1) We use orthographic camera 2) All art is broken up into power of two dimensions 3) We draw all the art in perspective 4) My level designers just drag and drop buildings, road etc, and they snap to a grid perfectly

Any thoughts on bringing a game like this to pass in Unity (grid based)?

alt text

1.jpg (17.3 kB)
Comment
Add comment · Show 1
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 Chronos-L · Jun 14, 2013 at 03:51 AM 0
Share

alt text

1) We use orthographic camera

No problem with this part. I have a ortographic camera positioned at ( 0, 5, -15 ), rotated at ( 45,0,0 ), with camera-size of 6 to get the effect in the screenshot.

All art is broken up into power of two dimensions

I am not exactly sure what you are talking about, but all the visible-object should have their size adhered to a specific unit-scale. For example, a basic grid will be 1 unit X 1 unit, a small building might be 5 unit X 3 unit, there should be no 1.5 unit or 1.2 units.

We draw all the art in perspective

To add on that, You can have 3D objects, it will seems 2D when it is viewed by the orthographic camera, a good thing with having a 3D object is that you can rotate it around to show a different side of the building, so a building can be reused to looks like 4 slightly different building (4 slightly different sides) if done correctly

$$anonymous$$y level designers just drag and drop buildings, road etc, and they snap to a grid perfectly

When you move stuff around in the editor, if you hold on Ctrl while dragging, the object will snap to a 1x1 grid. If you need a larger grid, or you want to not use the Ctrl, you will need to write your own editor script to do the calculation and snapping in the editor(a topic that I am not familiar with)

2 Replies

· Add your reply
  • Sort: 
avatar image
13

Answer by sotirosn · Jun 14, 2013 at 05:54 AM

you can make a script that executes in edit mode.

 [ExecuteInEditMode]
 public class EditModeGridSnap : MonoBehaviour {
     public float snapValue = 1;
     public float depth = 0;    
     
     void Update() {
         float snapInverse = 1/snapValue;
         
         float x, y, z;
         
         // if snapValue = .5, x = 1.45 -> snapInverse = 2 -> x*2 => 2.90 -> round 2.90 => 3 -> 3/2 => 1.5
         // so 1.45 to nearest .5 is 1.5
         x = Mathf.Round(transform.position.x * snapInverse)/snapInverse;
         y = Mathf.Round(transform.position.y * snapInverse)/snapInverse;   
         z = depth;  // depth from camera
         
         transform.position = new Vector3(x, y, z);
     }
 }

You may need to adjust the script depending on if objects are odd or even dimension, because it changes the centers, i.e. a cube .75 wide needs different math than a cube 1 unit wide when snapping to the nearest .25 units.

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 Kame Sama · Oct 03, 2014 at 03:07 AM 0
Share

Awesome script, I just used it for my level editor! thanks!

avatar image smokingbunny · Nov 28, 2014 at 11:41 AM 0
Share

id like to say 'thanks' for this code, it works great and fits what i need

avatar image d0ll · Feb 28, 2016 at 02:33 PM 0
Share

thank you for the code; [ExecuteInEdit$$anonymous$$ode] is fancy!

avatar image Naoweik · Nov 07, 2016 at 07:04 PM 0
Share

It's the end of 2016 and Unity still hasn't a grid snapping feature! Thank you for your script, very useful!! :)

avatar image michaelcalkins · Jan 18, 2018 at 06:54 AM 0
Share

This needs to be a feature in Unity.

avatar image
6

Answer by pozzoe · Sep 09, 2014 at 03:32 PM

I see there is a feature called Unit Snapping. Click ctrl while dragging and object will move by the amount defined on Edit->Snap Settings

Source

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 shieldgenerator7 · Mar 30, 2017 at 09:53 PM 0
Share

Yes, but in Unity 5.5, you have to select the movement tool for this to take effect (the tool bound to the "W" key by default).

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

22 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

Related Questions

2D Animation does not start 1 Answer

2D Grid puzzle game 1 Answer

Why is 200x100 Tile Taking 3 100x100 Grid Spaces? 0 Answers

Texture grid displayed oddly when width =/= height 1 Answer

2D Game Visual Grid to Place Objects 2 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