Skip to content

Instantly share code, notes, and snippets.

@dnkm
Created March 24, 2015 19:09
Show Gist options
  • Save dnkm/eb789d58204ee3ca882e to your computer and use it in GitHub Desktop.
Save dnkm/eb789d58204ee3ca882e to your computer and use it in GitHub Desktop.
Grid Snapping
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class SnapToGrid : MonoBehaviour {
public float cell_size = 1f; // = larghezza/altezza delle celle
private float x, y, z;
void Start() {
x = 0f;
y = 0f;
z = 0f;
}
void Update() {
// comment this out before deploying!
x = Mathf.Round(transform.position.x / cell_size) * cell_size;
y = Mathf.Round(transform.position.y / cell_size) * cell_size;
z = transform.position.z;
transform.position = new Vector3(x, y, z);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment