Created
March 26, 2018 10:37
-
-
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
This file contains 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; | |
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