Skip to content

Instantly share code, notes, and snippets.

@miguelSantirso
Created February 4, 2016 18:54
Show Gist options
  • Save miguelSantirso/5bfa8c3af9737d017bfb to your computer and use it in GitHub Desktop.
Save miguelSantirso/5bfa8c3af9737d017bfb to your computer and use it in GitHub Desktop.
[Unity] Auto snap to 2D grid
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