So I have an Editor script that allows you to put an object directly on top of the one below it. This is useful if you don’t want to go in and rotate around your object to make sure its sitting on a surface at the correct angle. What the script does is very simple. It raycasts down and if the raycast hits something, the object will be placed on the point where the raycast hits (hit.point) then it rotates the object so that it is flat on the hits normal. This works perfectly if you are moving an object with no volume…but obviously that’s not what I want. For example, with my current script, if I have a cube above a sphere and to the left a bit; with the cube selected, if I activate the button that performs the script, the cubes position will be perfectly on the hit.point and it will be rotated correctly. That’s not good enough though, because the cubes position is in the middle of the cubes mesh, so the cube is halfway inside of the sphere. So in conclusion, what can I do to find the distance I need to move the mesh so that it isn’t completely intersecting the mesh below. Also I guess I need to find the direction to offset. So distance and direction of offset, how do i calculate it? My current code is pretty simple…more simple that my explanation of the problem!
public class PlaceSelectedOnSurface extends ScriptableObject
{
@MenuItem ("Keenan's Cool Tools /Transform /Place Selected On Surface ")
static function CreateWizard ()
{
var transforms : Transform[] = Selection.GetTransforms(SelectionMode.Deep | SelectionMode.ExcludePrefab | SelectionMode.OnlyUserModifiable);
if (transforms.Length > 0 && EditorUtility.DisplayDialog("Move Selected Objects to The Surface Below?", "Are you sure, you are moving " + transforms.Length + " objects", "Yes", "No"))
{
var transform : Transform;
for (trans in transforms)
{
var hit : RaycastHit;
if (Physics.Raycast(trans.position, Vector3.down, hit))
{
var scale : Vector3 = trans.localScale;
trans.position = Vector3 (hit.point.x, hit.point.y, hit.point.z);
trans.rotation = Quaternion.LookRotation (trans.rotation.eulerAngles, hit.normal);
}
}
}
}
}