- Home /
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)?
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)
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.
Awesome script, I just used it for my level editor! thanks!
id like to say 'thanks' for this code, it works great and fits what i need
thank you for the code; [ExecuteInEdit$$anonymous$$ode] is fancy!
It's the end of 2016 and Unity still hasn't a grid snapping feature! Thank you for your script, very useful!! :)
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
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
Follow this Question
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