SHARE
TWEET

Unity Snap to Ground (more like Unreal)

burtonposey May 2nd, 2016 (edited) 346 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top