From Unify Community Wiki
Revision as of 06:22, 1 December 2005 by Aarku (Talk | contribs)

Jump to: navigation, search

Author: Jonathan Czeck (aarku)

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

This wizard takes the current selection and raycasts downward from each object and moves them to where they 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 ground.

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);
Personal tools