I want to spawn random objects over the terrain and also over the plane or only on one of them.
The spawning objects for the terrain are working but I'm not sure how to do it with the plane.
To start I can't even find how to get the plane size width and length.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SideQuests : MonoBehaviour
{
public Terrain terrain;
public Plane plane;
public GameObject prefab;
public GameObject parent;
[Range(10, 1000)]
public int numberOfObjects = 10;
public float yOffset = 10f;
private float terrainWidth;
private float terrainLength;
private float xTerrainPos;
private float zTerrainPos;
private float planeWidth;
private float planeLength;
private float xPlanePos;
private float zPlanePos;
void Start()
{
//Get terrain size
terrainWidth = terrain.terrainData.size.x;
terrainLength = terrain.terrainData.size.z;
//Get terrain position
xTerrainPos = terrain.transform.position.x;
zTerrainPos = terrain.transform.position.z;
//Get plane size
planeWidth = plane.
generateObjectOnTerrain();
}
void generateObjectOnTerrain()
{
for (int i = 0; i < numberOfObjects; i++)
{
//Generate random x,z,y position on the terrain
float randX = UnityEngine.Random.Range(xTerrainPos, xTerrainPos + terrainWidth);
float randZ = UnityEngine.Random.Range(zTerrainPos, zTerrainPos + terrainLength);
float yVal = Terrain.activeTerrain.SampleHeight(new Vector3(randX, 0, randZ));
//Apply Offset if needed
yVal = yVal + yOffset;
//Generate the Prefab on the generated position
GameObject objInstance = Instantiate(prefab, new Vector3(randX, yVal, randZ), Quaternion.identity);
objInstance.name = "Quest";
objInstance.tag = "Quest";
objInstance.transform.parent = parent.transform;
}
}
}