How can I find a Plane size and position and spawn random objects over it?

Viewed 498

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;
        }
    }
}
1 Answers

Your code uses Plane which has an infinite size:

A plane is an infinitely large, flat surface that exists in 3D space and divides the space into two halves known as half-spaces.

So to answer your question, the length and width of a Plane can be referenced with Mathf.Infinity

You can project any position to a location on the plane by using Plane.ClosestPointOnPlane. If you can generate random positions, such as with Random.insideUnitSphere you can generate random points on the plane that way:

float spawnRange = 50f;
Vector3 spawnCenter = Vector3.zero;
Vector3 randomPointOnPlane = plane.ClosestPointOnPlane(spawnCenter 
        + spawnRange * Random.insideUnitSphere);

(Note that if you use insideUnitSphere, you will have more spawns in the center of the sphere than the edges.)

Then, if you have a height you want them to spawn over the plane, you can add that:

float spawnHeight = 1f;
Vector3 randomPointOverPlane = Vector3.up * spawnHeight + randomPointOnPlane;
Related