实例化地形区域内的对象 [英] Instantiate objects within the terrain area

查看:57
本文介绍了实例化地形区域内的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Teleport : MonoBehaviour {

    public Vector3 terrainArea;
    public float spinSpeed = 2.0f;
    public int cloneTeleportations;
    public GameObject prefab;

    private bool rotate = false;
    private bool exited = false;
    private Transform[] teleportations;
    private Random rnd = new Random();

    private void Start()
    {

        GameObject go = GameObject.Find("Terrain");            
        Terrain terrain = go.GetComponent(Terrain);
        terrainArea = terrain.terrainData.size;

        for (int i = 0; i < cloneTeleportations; i++)
        {
            GameObject Teleportaion = Instantiate(prefab, new Vector3(Random.Range(i * 10.0F, i * 50.0F), 0, Random.Range(i * 10.0F, i * 50.0F)), Quaternion.identity);
            Teleportaion.transform.parent = this.transform;
            Teleportaion.transform.tag = "Teleportation";
        }
    } 
}

现在,一些游戏对象不在地形区域内. 我要做的是将克隆保持在随机位置,但只能在地形区域内.

Now some gameobjects are out of the terrain area. What I want to do is to keep the clones to be in random position but only inside the terrain area.

如何实例化地形区域内的对象?

How I do Instantiate Objects inside the terrain area?

推荐答案

我注意到您正在创建Random类的新实例.不要使用Unity的 Random API来做到这一点.只需使用 Random.Range 即可生成随机数.

I noticed that you are creating new instance of the Random class. Don't do that with Unity's Random API. Just use Random.Range to generate random numbers.

关于在地形上生成随机GameObject:

As for generating random GameObjects on a terrain:

1 .找到X位置:

1.Find the X position:

Min = terrain.terrainData.size.x.

Max = terrain.transform.position.x + terrain.terrainData.size.x.

最终的随机X应该是:

randX = UnityEngine.Random.Range(Min, Min + Max);


2 .找到Z位置:


2.Find the Z position:

Min = terrain.transform.position.z;

Max = terrain.transform.position.z + terrain.terrainData.size.z;

最终的随机Z应该是:

 randZ = UnityEngine.Random.Range(Min, Min + Max);


3 .找到Y位置:


3.Find the Y position:

y轴不必是随机的.虽然,您可以根据需要将其设为随机值.

The y-axis does not have to be random. Although, you can make it a random value if you want.

它只必须在地形上,这样实例化的对象才不会在地形下.

It just has to be over the terrain so that the Instantiated Object will not be under the terrain.

这是 Terrain.SampleHeight 放置的位置.您必须使用此功能才能完美生成不在地形下方的位置.您需要#1 #2 的结果才能使用此功能:

This is where Terrain.SampleHeight comes into place. You must use this function in order to perfectly generate a position that is not under the terrain. You need the result from #1 and #2 to use this:

yVal = Terrain.activeTerrain.SampleHeight(new Vector3(randX, 0, randZ));

如果要将偏移量应用于y轴,则现在可以执行此操作.

If you want to apply offset to the y-axis, you can now do it at this time.

yVal = yVal + yOffset;


下面是一个一般示例.您必须使用此示例来扩展当前代码.


Below is a general example. You have to use this example to extend your current code.

public GameObject prefab;
public Terrain terrain;
public float yOffset = 0.5f;

private float terrainWidth;
private float terrainLength;

private float xTerrainPos;
private float zTerrainPos;


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;

    generateObjectOnTerrain();
}

void generateObjectOnTerrain()
{
    //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 = (GameObject)Instantiate(prefab, new Vector3(randX, yVal, randZ), Quaternion.identity);
}

这篇关于实例化地形区域内的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆