简体   繁体   English

如何找到平面大小和 position 并在其上生成随机对象?

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

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

Your code uses Plane which has an infinite size:您的代码使用具有无限大小的Plane

A plane is an infinitely large , flat surface that exists in 3D space and divides the space into two halves known as half-spaces.平面是一个无限大的平面,存在于 3D 空间中,并将空间分成称为半空间的两半。

So to answer your question, the length and width of a Plane can be referenced with Mathf.Infinity所以要回答你的问题, Plane的长度和宽度可以参考Mathf.Infinity

You can project any position to a location on the plane by using Plane.ClosestPointOnPlane .您可以使用Plane.ClosestPointOnPlane将任何 position 投影到平面上的某个位置。 If you can generate random positions, such as with Random.insideUnitSphere you can generate random points on the plane that way:如果您可以生成随机位置,例如使用Random.insideUnitSphere ,您可以通过这种方式在平面上生成随机点:

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.) (请注意,如果您使用insideUnitSphere ,球体中心的生成点将多于边缘。)

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM