I was looking for a simple way to place objects on the ground or surface below the object and couldn’t seem to find any way in Unity to do it easily.
Here is an editor script that you can use to place the selected object on the surface below it. Just put this code into a file called DropObjectEditor.js in your Editor folder.
When you run it a small window opens that lets you place it by bottom, origin or center and you can simply dock the window in your editor and it’s always available then.
Please let me know of any bugs.
Cheers
EDIT: Realized multi-select dropping was trivial and still worked for single selections, so changed the code to drop multiple items. Easier to populate a large area or put items on a table…
class DropObjectEditor extends EditorWindow
{
// add menu item
@MenuItem ("Window/Drop Object")
static function Init ()
{
// Get existing open window or if none, make a new one:
var window : DropObjectEditor = EditorWindow.GetWindow(DropObjectEditor);
window.Show ();
}
function OnGUI ()
{
GUILayout.Label ("Drop Using:", EditorStyles.boldLabel);
GUILayout.BeginHorizontal();
if(GUILayout.Button("Bottom"))
{
DropObjects("Bottom");
}
if(GUILayout.Button("Origin"))
{
DropObjects("Origin");
}
if(GUILayout.Button("Center"))
{
DropObjects("Center");
}
GUILayout.EndHorizontal();
}
function DropObjects(method : String)
{
// drop multi-selected objects using the right method
for(var i : int = 0; i < Selection.transforms.length; i++)
{
// get the game object
var go : GameObject = Selection.transforms[i].gameObject;
// don't think I need to check, but just to be sure...
if(!go)
{
continue;
}
// get the bounds
var bounds : Bounds = go.renderer.bounds;
var hit : RaycastHit;
var yOffset : float;
// override layer so it doesn't hit itself
var savedLayer : int = go.layer;
go.layer = 2; // ignore raycast
// see if this ray hit something
if(Physics.Raycast(go.transform.position, -Vector3.up, hit))
{
// determine how the y will need to be adjusted
switch(method)
{
case "Bottom":
yOffset = go.transform.position.y - bounds.min.y;
break;
case "Origin":
yOffset = 0.0;
break;
case "Center":
yOffset = bounds.center.y - go.transform.position.y;
break;
}
go.transform.position = hit.point;
go.transform.position.y += yOffset;
}
// restore layer
go.layer = savedLayer;
}
}
}