- Home /
Terrain Brush for buildings instead for trees bushes etc.
So you can put trees and bushes and rocks on a terrain but is it possible to place a building on my terrain, because I have an Infiniteterrain script that copies the terrain as you walk turns it etc. but the world is kinda boring to explore without buildings (with loot etc) I searched for procedurall building assets but their are no free ones
I don't see any reason why you cannot add a building to your terrain assets in the same way as you would add a bush or a tree, it's just a mesh at the end of the day...
Well the problem is if i want for example working doors or item spawnpoint I cant do that because its required to be one whole object Edit: what i mean is you cant have childs
Answer by KMKxJOEY1 · Nov 04, 2014 at 07:42 PM
No you cannot have prefabs as your mesh for the terrain editor. What I did to get around a similar problem was code my own editor script that automatically mass placed a prefab for me. These are the related files:
using UnityEngine;
public class StarPlacementHelper : MonoBehaviour
{
public Color starTint;
public Color starTint2;
public float minDensity;
public float maxDensity;
public Bounds bounds;
public SpriteRenderer[] stars;
public Vector2 minSize;
public Vector2 maxSize;
public bool randomRotation;
public void Place()
{
if (minDensity > maxDensity || maxDensity < 0)
return;
float xHalfExtent = ((float)bounds.extents.x / 2);
float yHalfExtent = ((float)bounds.extents.y / 2);
float zHalfExtent = ((float)bounds.extents.z / 2);
for (float x = bounds.center.x - xHalfExtent; x < bounds.center.x + xHalfExtent; x += getRandomDensity())
{
for (float y = bounds.center.y - yHalfExtent; y < bounds.center.y + yHalfExtent; y += getRandomDensity())
{
//skip factor
if (Random.Range(0, 9) > 1) //80 percent
continue;
for (float z = bounds.center.z - zHalfExtent; z < bounds.center.z + zHalfExtent; z += getRandomDensity())
{
//skip factor
if (Random.Range(0, 9) > 1) //80 percent
continue;
int index = Random.Range(0, stars.Length);
Color col = Color.Lerp(starTint, starTint2, Random.Range(0f, 1f));
SpriteRenderer r = Instantiate(stars[index], new Vector3(x, y, z), Quaternion.identity) as SpriteRenderer;
r.color = col;
r.transform.localScale = getRandomSize();
r.transform.parent = transform;
if (randomRotation)
r.transform.eulerAngles = new Vector3(0, 0, Random.Range(0f, 359f));
}
}
}
}
public void DeleteChildren()
{
//foreach (Transform t in transform)
// DestroyImmediate(t.gameObject);
foreach (SpriteRenderer t in transform.GetComponentsInChildren<SpriteRenderer>())
DestroyImmediate(t.gameObject);
}
private Vector3 getRandomSize()
{
//we want a square anyways
float x = Random.Range(minSize.x, maxSize.x) + minSize.x;
//float y = Random.Range(minSize.y, maxSize.y) + minSize.y;
//return new Vector3(x, y, 0);
return new Vector3(x, x, 0);
}
private float getRandomDensity()
{
return Random.Range(minDensity, maxDensity);
}
void OnDrawGizmosSelected()
{
Gizmos.color = new Color(1, 1, 1, 0.3f);
Gizmos.DrawCube(bounds.center - Vector3.forward * 20, bounds.extents);
}
}
///////////////
//place this one inside of an editor folder:
///////////////
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(StarPlacementHelper))]
public class StarPlacement : Editor
{
public override void OnInspectorGUI()
{
serializedObject.Update();
StarPlacementHelper s = target as StarPlacementHelper;
base.OnInspectorGUI();
if (GUILayout.Button("Populate"))
{
s.Place();
}
else if (GUILayout.Button("Delete Children"))
{
s.DeleteChildren();
}
}
private static void Space(int num = 1)
{
for (int i = 0; i < num; i++)
EditorGUILayout.Space();
}
}
Keep in mind that this was for a 2D project, you may have to change up some of the axis to get what you are looking for.
Are these 2 scripts in one since one has to be put in the editor folder? Also isn't it possible to add the gameobjects with lootpoint etc then convert all children to make it one solid single parent object?
Your answer
Follow this Question
Related Questions
Terrain not appearing shaded unless very close 1 Answer
Terrain matrix 8*8 1 Answer
duplicate terrain and change painted tree asset of only the duplicate 0 Answers
help with terrains 1 Answer
terrain engine with holes 0 Answers