Finished most of my terrain generation stuff, but need help making the random generation script spawn/instantiate objects and trees only on a specific texture on my terrain…
Can someone please help me add the splat/alpha part since i’v tried for hours and given up.
Any help is appreciated, thanks!
script:
using UnityEngine;
using System.Collections;
public class RandomObjects : MonoBehaviour
{
public Terrain terrain;
public int numberOfObjects; // number of objects to place
private int currentObjects; // number of placed objects
public GameObject objectToPlace; // GameObject to place
private int terrainWidth; // terrain size (x)
private int terrainLength; // terrain size (z)
private int terrainPosX; // terrain position x
private int terrainPosZ; // terrain position z
void Start()
{
terrain = gameObject.GetComponent<Terrain> ();
// terrain size x
terrainWidth = (int)terrain.terrainData.size.x;
// terrain size z
terrainLength = (int)terrain.terrainData.size.z;
// terrain x position
terrainPosX = (int)terrain.transform.position.x;
// terrain z position
terrainPosZ = (int)terrain.transform.position.z;
for (int t =0; t <50; t++){
if(currentObjects <= numberOfObjects)
{
// generate random x position
int posx = Random.Range(terrainPosX, terrainPosX + terrainWidth);
// generate random z position
int posz = Random.Range(terrainPosZ, terrainPosZ + terrainLength);
// get the terrain height at the random position
float posy = Terrain.activeTerrain.SampleHeight(new Vector3(posx, 0, posz));
// create new gameObject on random position
GameObject newObject = (GameObject)Instantiate(objectToPlace, new Vector3(posx, posy, posz), Quaternion.identity);
currentObjects += 1;
}
if(currentObjects == numberOfObjects)
{
Debug.Log("Generate objects complete!");
}
}
}
// Update is called once per frame
void Update()
{
// generate objects
}
}