Created
December 18, 2019 15:20
-
-
Save unitycoder/ba1d9aff769877af926e6f8101760167 to your computer and use it in GitHub Desktop.
Drop Object To Terrain (Editor Script)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from package https://assetstore.unity.com/packages/tools/terrain/tom-s-terrain-tools-527 | |
using UnityEngine; | |
using UnityEditor; | |
namespace TTT | |
{ | |
public class PlaceOnTerrain : ScriptableObject | |
{ | |
public Texture2D lmap; | |
[MenuItem ("Window/Terrain Tools/Drop Selection to Terrain",false,103)] | |
static void PlaceSelectionOnTerrain() | |
{ | |
foreach (Transform t in Selection.transforms) | |
{ | |
if (t.GetComponent<Terrain>()==null) | |
{ | |
Undo.RecordObject(t, "Move " + t.name); | |
RaycastHit hit; | |
if (Physics.Raycast(t.position, -Vector3.up, out hit)) { | |
float distanceToGround = hit.distance; | |
t.Translate(-Vector3.up * distanceToGround); | |
} else if (Physics.Raycast(t.position, Vector3.up, out hit)) { | |
float distanceToGround = hit.distance; | |
t.Translate(Vector3.up * distanceToGround); | |
} | |
} | |
} | |
} | |
[MenuItem ("Window/Terrain Tools/Drop Selection to Terrain", true, 3)] | |
static bool ValidatePlaceSelectionOnTerrain () { | |
return Selection.activeTransform != null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment