PlaceSelectionOnSurface

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
Line 22: Line 22:
 
  public class PlaceSelectionOnSurface : ScriptableObject
 
  public class PlaceSelectionOnSurface : ScriptableObject
 
  {
 
  {
[MenuItem ("GameObject/Place Selection On Surface")]
+
    [MenuItem ("GameObject/Place Selection On Surface")]
static void CreateWizard ()
+
    static void CreateWizard ()
{
+
    {
Transform[] transforms = Selection.GetTransforms(SelectionMode.Deep |
+
        Transform[] transforms = Selection.GetTransforms(SelectionMode.Deep |
SelectionMode.ExcludePrefab | SelectionMode.OnlyUserModifyable);
+
            SelectionMode.ExcludePrefab | SelectionMode.OnlyUserModifyable);
+
       
if (transforms.Length > 0 && EditorUtility.DisplayDialog("Place Selection On Surface?",
+
        if (transforms.Length > 0 && EditorUtility.DisplayDialog("Place Selection On Surface?",
"Are you sure you want to place " + transforms.Length +
+
            "Are you sure you want to place " + transforms.Length +
((transforms.Length > 1) ? " objects" : " object") +
+
            ((transforms.Length > 1) ? " objects" : " object") +
" on the surface in the -Y direction?", "Place", "Do Not Place"))
+
            " on the surface in the -Y direction?", "Place", "Do Not Place"))
{
+
        {
foreach (Transform transform in transforms)
+
            foreach (Transform transform in transforms)
{
+
            {
RaycastHit hit;
+
                RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit))
+
                if (Physics.Raycast(transform.position, Vector3.down, out hit))
{
+
                {
transform.position = hit.point;
+
                    transform.position = hit.point;
Vector3 randomized = Random.onUnitSphere;
+
                    Vector3 randomized = Random.onUnitSphere;
randomized = new Vector3(randomized.x, 0F, randomized.z);
+
                    randomized = new Vector3(randomized.x, 0F, randomized.z);
transform.rotation = Quaternion.LookRotation(randomized, hit.normal);
+
                    transform.rotation = Quaternion.LookRotation(randomized, hit.normal);
}
+
                }
}
+
            }
}
+
        }
}
+
    }
 
  }
 
  }
 
</csharp>
 
</csharp>

Revision as of 08:47, 2 December 2005

Author: Jonathan Czeck (aarku)

Description

This wizard takes the current selection and raycasts downward from each object and moves them to where the raycast hit. It is useful for placing a lot of items on top of a terrain.

It is not bulletproof to say the least, but it provides a starting point.

Usage

Place this script in YourProject/Assets/Editor and a menu item will automatically appear in the GameObject menu after it is compiled.

Note: This snippet requires at least Unity version 1.2

C# - PlaceSelectionOnSurface.cs

<csharp>

using UnityEngine;
using UnityEditor;

public class PlaceSelectionOnSurface : ScriptableObject
{
   [MenuItem ("GameObject/Place Selection On Surface")]
   static void CreateWizard ()
   {
       Transform[] transforms = Selection.GetTransforms(SelectionMode.Deep |
           SelectionMode.ExcludePrefab | SelectionMode.OnlyUserModifyable);
       
       if (transforms.Length > 0 && EditorUtility.DisplayDialog("Place Selection On Surface?",
           "Are you sure you want to place " + transforms.Length +
           ((transforms.Length > 1) ? " objects" : " object") +
           " on the surface in the -Y direction?", "Place", "Do Not Place"))
       {
           foreach (Transform transform in transforms)
           {
               RaycastHit hit;
               if (Physics.Raycast(transform.position, Vector3.down, out hit))
               {
                   transform.position = hit.point;
                   Vector3 randomized = Random.onUnitSphere;
                   randomized = new Vector3(randomized.x, 0F, randomized.z);
                   transform.rotation = Quaternion.LookRotation(randomized, hit.normal);
               }
           }
       }
   }
}

</csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox