Created
February 4, 2016 18:54
-
-
Save miguelSantirso/5bfa8c3af9737d017bfb to your computer and use it in GitHub Desktop.
[Unity] Auto snap to 2D grid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
[ExecuteInEditMode] | |
public class AutoSnapToGrid2D : MonoBehaviour | |
{ | |
public Vector2 gridSize = new Vector2(0.32f, 0.32f); | |
void Update() | |
{ | |
Vector3 localPos = transform.localPosition; | |
localPos.x = Mathf.Round(localPos.x / gridSize.x) * gridSize.x; | |
localPos.y = Mathf.Round(localPos.y / gridSize.y) * gridSize.y; | |
transform.localPosition = localPos; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment