Ok, so I am making an editor script that will move all of the selected objects onto the surface of the object directly below.
I came up with the following code:
@MenuItem("Scripts/Normalize")
static function Normalize() {
for (var obj : GameObject in Selection.gameObjects)
{
var hit : RaycastHit;
if(Physics.Raycast(obj.transform.position, -Vector3.up, hit))
{
var coll : Collider = obj.GetComponent(Collider);
var bottom = coll.bounds.max.y
obj.transform.position.y = obj.transform.position.y - hit.distance + bottom;
}
}
}
If I remove the bottom variable from the vertical translation, it results in the selections center of gravity being placed at the surface. With the bottom variable being taken into account, however, it doesn’t even make it to the ground… it moves downward slightly, but not to where it should.
Am I doing something wrong or is there a better way to detect the bottom of an object?