当前脚本所在的对象下的地形,该地形将达到最大高度。我可以以某种方式将其设置为指定高度的变量吗?
我没有尝试太多,因为我不知道这怎么做都很好。
using System.Collections; using System.Collections.Generic; using
UnityEngine;
public class test : MonoBehaviour {
public Terrain terr; // terrain to modify
int hmWidth; // heightmap width
int hmHeight; // heightmap height
int posXInTerrain; // position of the game object in terrain width (x axis)
int posYInTerrain; // position of the game object in terrain height (z axis)
int size = 5; // the diameter of terrain portion that will raise under the game object
float desiredHeight = 2; // the height we want that portion of terrain to be
void Start()
{
terr = Terrain.activeTerrain;
hmWidth = terr.terrainData.heightmapWidth;
hmHeight = terr.terrainData.heightmapHeight;
}
void Update()
{
// get the normalized position of this game object relative to the terrain
Vector3 tempCoord = (transform.position - terr.gameObject.transform.position);
Vector3 coord;
coord.x = tempCoord.x / terr.terrainData.size.x;
coord.y = tempCoord.y / terr.terrainData.size.y;
coord.z = tempCoord.z / terr.terrainData.size.z;
// get the position of the terrain heightmap where this game object is
posXInTerrain = (int)(coord.x * hmWidth);
posYInTerrain = (int)(coord.z * hmHeight);
// we set an offset so that all the raising terrain is under this game object
int offset = size / 2;
// get the heights of the terrain under this game object
float[,] heights = terr.terrainData.GetHeights(posXInTerrain - offset, posYInTerrain - offset, size, size);
// we set each sample of the terrain in the size to the desired height
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
heights[i, j] = desiredHeight;
// set the new height
terr.terrainData.SetHeights(posXInTerrain - offset, posYInTerrain - offset, heights);
}
}
我希望有一个输入并将高度设置为输入的高度。
示例: 输入= 10 物体下方的地形变为10
实际输出: 输入-10 物体下方的地形达到600
答案 0 :(得分:0)
您传递给SetHeights
方法的高度应在0到1之间。0表示0,1表示地形的最大高度。
var heightScale = 1.0f / terr.terrainData.size.y;
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
heights[i, j] = desiredHeight * heigthScale;