Advertisement
burtonposey

Unity Snap to Ground (more like Unreal)

May 2nd, 2016
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. public class TransformMacros : Editor
  5. {
  6.     [MenuItem("Macros/Snap Selection To Ground #END")]
  7.     public static void SnapToGround()
  8.     {
  9.         foreach (Transform t in Selection.transforms)
  10.         {
  11.             RaycastHit rayhit;
  12.             if (Physics.Raycast(t.position, Vector3.down, out rayhit))
  13.             {
  14.                 Vector3 offset = Vector3.zero;
  15.                 MeshRenderer renderer = t.GetComponentInChildren<MeshRenderer>();
  16.                 if (renderer != null)
  17.                 {
  18.                     if (renderer.bounds.center.y - renderer.bounds.extents.y < t.position.y)
  19.                     {
  20.                         offset = new Vector3(0, renderer.bounds.extents.y, 0);
  21.                     }
  22.                 }
  23.                 t.position = rayhit.point + offset;
  24.             }
  25.         }
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement