Last active
September 3, 2020 09:37
-
-
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)
This file contains hidden or 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 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