- Home /
spawning/instantiating based on a specific texture on terrain
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
  
     }
 }
Solved this problem... thanks a lot Alucardj!! Ad$$anonymous$$ Please close this thread/question..
What was your solution? I am trying to do the same thing.
Answer by LuckyStreak · Jan 21, 2017 at 06:52 AM
What was the solution to your problem? I am trying to do the same thing.
Your answer
 
 
             Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Generating Structures on Infinite Terrain 1 Answer
Vector math problem 2 Answers
Procedural terrain bigger than I set it, heightmap slightly off 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                