-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChangeTerrainHeight.cs
More file actions
54 lines (43 loc) · 1.84 KB
/
ChangeTerrainHeight.cs
File metadata and controls
54 lines (43 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeTerrianHeight : MonoBehaviour {
public Terrain theTerrain;
int WidthTer;
int HeightTer;
//unity 預設的高度倍率,在terrain.setting.resolution裡面
float muti = 600;
void Start(){
theTerrain = Terrain.activeTerrain;
WidthTer = theTerrain.terrainData.heightmapWidth;
HeightTer = theTerrain.terrainData.heightmapHeight;
changeTerrain();
}
void changeTerrain(){
//heights指的是在此terrian上的所有點的height數值,先初始擷取所有點
float[,] heights = theTerrain.terrainData.GetHeights(0 , 0 , WidthTer , HeightTer);
Debug.Log(WidthTer);
heights = makeCircleHeights(heights , 100 , 100 , 50 , 10.0f / muti);
heights = makeCircleHeights(heights, 100, 100, 40, 0.0f / muti);
heights = makeRectangleHeights(heights, 50, 95, 90 , 10 , 10.0f / muti);
theTerrain.terrainData.SetHeights(0,0,heights);
}
float[,] makeCircleHeights(float[,] heights , int posX , int posZ , int radius , float setHeight){
for(int z = posZ - radius; z < posZ + radius; z++){
for(int x = posX - radius; x < posX + radius; x++){
if(Mathf.Pow (posZ - z, 2) + Mathf.Pow (posX - x, 2) < Mathf.Pow(radius , 2)){
heights[x, z] = setHeight;
}
}
}
return heights;
}
float[,] makeRectangleHeights(float[,] heights, int startX, int startZ , int theLong , int width , float setHeight){
for (int z = startZ; z < startZ + width; z++){
for (int x = startX; x < startX + theLong; x++){
heights[x, z] = setHeight;
}
}
return heights;
}
}