using UnityEngine; using System.Collections; public class SpawnInArea : MonoBehaviour { public Texture2D SpawnMap; float Offset = 10.0f; float AboveGround = 1.0f; bool TerrainOnly = true; // Use this for initialization void RandomPositionOnTerrain(GameObject obj) { Vector3 size = Terrain.activeTerrain.terrainData.size; Vector3 NewPosition = new Vector3(); bool done = false; while (!done) { // calculate new random position NewPosition = Terrain.activeTerrain.transform.position; float w = Random.Range(0.0f, size.x); float h = Random.Range(0.0f, size.z); NewPosition.x += w; NewPosition.y += size.y + Offset; // make sure we are above the terrain NewPosition.z += h; // verify that position is in spawnmap if (SpawnMap) { int xmap = Mathf.RoundToInt((float)SpawnMap.width * w/size.x); int ymap = Mathf.RoundToInt((float)SpawnMap.height * h/size.z); float value = SpawnMap.GetPixel(xmap, ymap).grayscale; if (value>0.0f && Random.Range(0.0f, 1.0f)