Skip to content

Instantly share code, notes, and snippets.

@louis-e
Last active September 3, 2020 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louis-e/db0feb39581e80815bc09ff6ede325b2 to your computer and use it in GitHub Desktop.
Save louis-e/db0feb39581e80815bc09ff6ede325b2 to your computer and use it in GitHub Desktop.
GridSnapping.cs | GameObject Editor Grid Snapping (Optional: change the float gridSize to set the size of the grid)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class GridSnapping : MonoBehaviour
{
public GameObject target;
Vector3 gridPos;
public float gridSize = 10f;
private void Awake()
{
gridSize = target.transform.localScale.x * 10f;
}
void LateUpdate()
{
gridPos.x = Mathf.Floor(target.transform.position.x / gridSize) * gridSize;
gridPos.y = Mathf.Floor(target.transform.position.y / gridSize) * gridSize;
gridPos.z = Mathf.Floor(target.transform.position.z / gridSize) * gridSize;
target.transform.position = gridPos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment