Skip to content

Instantly share code, notes, and snippets.

@misato
Created March 26, 2018 10:37
Show Gist options
  • Save misato/03a89a32b2424754cc07533040dd236f to your computer and use it in GitHub Desktop.
Save misato/03a89a32b2424754cc07533040dd236f to your computer and use it in GitHub Desktop.
Script to Snap to Grid objects in Unity, so it's pixel perfect
using UnityEngine;
public class SnapToGrid : MonoBehaviour {
public float PPU = 19; // pixels per unit (your tile size)
private void LateUpdate() {
Vector3 position = transform.localPosition;
position.x = (Mathf.Round(transform.parent.position.x * PPU) / PPU) - transform.parent.position.x;
position.y = (Mathf.Round(transform.parent.position.y * PPU) / PPU) - transform.parent.position.y;
transform.localPosition = position;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment