Skip to content

Instantly share code, notes, and snippets.

@Petethegoat
Last active November 2, 2016 08:31
Show Gist options
  • Save Petethegoat/2eaedc1b8768161fe33e to your computer and use it in GitHub Desktop.
Save Petethegoat/2eaedc1b8768161fe33e to your computer and use it in GitHub Desktop.
Editor script for Unity that drops the selected gameobject to the ground, based on it's extents. Doesn't handle convex shapes at all. Put in Assets/Editor/DropToFloor.cs
using UnityEngine;
using UnityEditor;
namespace Goodfellow.Utils
{
public class DropToFloor
{
/*
These are the supported keys (can also be combined together):
% – CTRL on Windows / CMD on OSX
# – Shift
& – Alt
LEFT/RIGHT/UP/DOWN – Arrow keys
F1…F2 – F keys
HOME, END, PGUP, PGDN
Character keys not part of a key-sequence are added by adding an underscore prefix to them (e.g: _g for shortcut key “G”).
*/
[MenuItem("Edit/Drop to Floor _f")]
private static void DoDropToFloor()
{
Transform active = Selection.activeTransform;
if(active == null)
return;
Renderer renderer = active.GetComponent<Renderer>();
if(renderer == null)
return;
Bounds bounds = renderer.bounds;
RaycastHit[] hits;
hits = Physics.BoxCastAll(bounds.center, bounds.extents, Vector3.down, Quaternion.identity, 100f);
float highest = active.position.y - 100;
foreach(RaycastHit hit in hits)
{
if(hit.transform == active)
{
continue;
}
if(hit.point.y > highest)
{
highest = hit.point.y;
}
}
Undo.RecordObject(active, "Drop to Floor");
active.position = new Vector3(active.position.x, highest, active.position.z);
active.Translate(Vector3.up * renderer.bounds.extents.y);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment