C# 我怎样才能找到一个平面的大小和位置,并在上面生成随机对象?

C# 我怎样才能找到一个平面的大小和位置,并在上面生成随机对象?,c#,unity3d,C#,Unity3d,我想在地形上、平面上或仅在其中一个上生成随机对象 地形的繁殖对象正在工作,但我不确定如何使用飞机 首先,我甚至找不到如何获得平面大小、宽度和长度 using System.Collections; using System.Collections.Generic; using UnityEngine; public class SideQuests : MonoBehaviour { public Terrain terrain; public Plane plane;

我想在地形上、平面上或仅在其中一个上生成随机对象

地形的繁殖对象正在工作,但我不确定如何使用飞机

首先,我甚至找不到如何获得平面大小、宽度和长度

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;
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共阶级的附带任务:单一行为
{
公共地形;
公共飞机;
公共游戏对象预制;
公共游戏对象父对象;
[射程(101000)]
公共int numberOfObjects=10;
公共浮动Y偏移=10f;
私有浮动地形宽度;
私人浮动平台长度;
私人浮动xTerrainPos;
私人浮动zTerrainPos;
私人浮动平面宽度;
私人浮动长度;
私人浮动xPlanePos;
私人浮动zPlanePos;
void Start()
{
//获取地形大小
terrainWidth=terrain.terrainData.size.x;
terrainLength=terrain.terrainData.size.z;
//获取地形位置
xTerrainPos=terrain.transform.position.x;
zTerrainPos=地形.transform.position.z;
//获取平面大小
平面宽度=平面。
generateObjectInterrain();
}
void generateObjectInterrain()
{
for(int i=0;i
您的代码使用无限大的:

平面是一个无限大的平面,存在于三维空间中,将空间分成两半,称为半空间

因此,为了回答您的问题,
平面的长度和宽度可以用

可以使用将任何位置投影到平面上的某个位置。如果可以生成随机位置,例如使用,则可以通过这种方式在平面上生成随机点:

float spawnRange = 50f;
Vector3 spawnCenter = Vector3.zero;
Vector3 randomPointOnPlane = plane.ClosestPointOnPlane(spawnCenter 
        + spawnRange * Random.insideUnitSphere);
(请注意,如果使用
insideUnitSphere
,则球体中心的繁殖将多于边缘。)

然后,如果希望它们在平面上繁殖的高度,可以添加:

float spawnHeight = 1f;
Vector3 randomPointOverPlane = Vector3.up * spawnHeight + randomPointOnPlane;
您的代码使用具有无限大小的:

平面是一个无限大的平面,存在于三维空间中,将空间分成两半,称为半空间

因此,为了回答您的问题,
平面的长度和宽度可以用

可以使用将任何位置投影到平面上的某个位置。如果可以生成随机位置,例如使用,则可以通过这种方式在平面上生成随机点:

float spawnRange = 50f;
Vector3 spawnCenter = Vector3.zero;
Vector3 randomPointOnPlane = plane.ClosestPointOnPlane(spawnCenter 
        + spawnRange * Random.insideUnitSphere);
(请注意,如果使用
insideUnitSphere
,则球体中心的繁殖将多于边缘。)

然后,如果希望它们在平面上繁殖的高度,可以添加:

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