效果演示:
效果演示
一、柏林噪声动态地形 (BerlinNoiseGround.cs)
通过在运行时不断调整地形高度,创建出动态变化的自然地形效果。
柏林噪声+指数缩放
定义变量:
public Vector3 height;
public float minNoise;
public float maxNoise;
public GameObject noiseGround;
public float noise;
public float scale;
// 添加控制速度的参数
public float scaleSpeed;
public float powBase; // 对数变化的基数
public float timeMultiplier; // 时间缩放系数
private float elapsedTime; // 初始时间
初始化:
private void OnEnable()
{
timeMultiplier = 0.8f;
scale = 5f;
scaleSpeed = 1f;
powBase = 1.01f;
elapsedTime = Time.time;
}
更新:
private void Update()
{
float currentTime = Time.time - elapsedTime;
// 指数增长公式:logTime = powBase^(currentTime * timeMultiplier)
float logTime = Mathf.Pow(powBase, currentTime * timeMultiplier);
// 更新比例尺(指数增长)
scale += scaleSpeed * logTime;
// 计算柏林噪声
noise = Mathf.PerlinNoise(
transform.position.x / scale,
transform.position.z / scale
);
// 计算并应用高度
float heightY = Mathf.Lerp(minNoise, maxNoise, noise);
height = new Vector3(3, heightY, 3);
noiseGround.transform.localScale = height;
}
- 使用指数计算确保地形变化前期快、后期平稳
- 根据网格位置生成地形高度
scale
随时间增加使地形变化逐渐平缓- 不用三角函数是这样在场景中更有随机性而不是简单的周期结构
- 挂载在每一个地面物体上
二、智能地面区块管理 (MoveGround.cs)
数据定义与初始化:
public GameObject player;
public List<GameObject> childrens;
public List<GameObject> closedGameObject;
public List<Vector2Int> childrensPosition;
private void Awake()
{
for (int i = 0; i < transform.childCount; i++)
{
childrens.Add(transform.GetChild(i).gameObject);
childrensPosition.Add(new Vector2Int((int)transform.GetChild(i).position.x,(int)transform.GetChild(i).position.z));
}
}
根据角色更新地面物体位置:
if (player.transform.localScale.x > 0)
{
foreach (GameObject child in childrens)
{
if (!child.activeSelf) continue;
Vector2 playerPosition = new Vector2(player.transform.position.x, player.transform.position.z);
Vector2 childPosition = new Vector2(child.transform.position.x, child.transform.position.z);
if ((childPosition.x - playerPosition.x) < -5f)
{
childrensPosition.Remove(new Vector2Int((int)child.transform.position.x, (int)child.transform.position.z));
closedGameObject.Add(child);
child.SetActive(false);
BuildGameObject();
}
}
}
if (player.transform.localScale.x < 0)
{
foreach (GameObject child in childrens)
{
if (!child.activeSelf) continue;
Vector2 playerPosition = new Vector2(player.transform.position.x, player.transform.position.z);
Vector2 childPosition = new Vector2(child.transform.position.x, child.transform.position.z);
if ((childPosition.x - playerPosition.x) > 5f)
{
childrensPosition.Remove(new Vector2Int((int)child.transform.position.x, (int)child.transform.position.z));
closedGameObject.Add(child);
child.SetActive(false);
BuildGameObject();
}
}
}
改变物体位置方法:
private void BuildGameObject()
{
GameObject a=null;
if (closedGameObject.Count > 0)
{
a = closedGameObject[0];
closedGameObject.RemoveAt(0);
}
GameObject go = AddGameObject();
Vector2 vector2 = ActiveGameObject(go);
a.transform.position = new Vector3(vector2.x, 0, vector2.y);
a.SetActive(true);
childrensPosition.Add(new Vector2Int((int)a.transform.position.x, (int)a.transform.position.z));
}
遍历所有物体并返回处于边缘位置的物体的值:
private GameObject AddGameObject()
{
GameObject theGo=null;
float minPo = 999f;
foreach(GameObject child in childrens)
{
if(CheakHasGameObject(child.transform.position.x, child.transform.position.z))
{
continue;
}
float a = (child.transform.position - player.transform.position).magnitude;
if(a < minPo)
{
minPo = a ;
theGo = child;
}
}
if (theGo != null)
{
return theGo;
}
return null;
}
查看8邻域是否有物体:
private bool CheakHasGameObject(float x,float z)
{
int eP = 0;
if (childrensPosition.Exists(pos => pos == new Vector2(x + 3, z + 3))) eP += 1;
if (childrensPosition.Exists(pos => pos == new Vector2(x + 3, z))) eP += 1;
if (childrensPosition.Exists(pos => pos == new Vector2(x + 3, z - 3))) eP += 1;
if (childrensPosition.Exists(pos => pos == new Vector2(x, z + 3))) eP += 1;
if (childrensPosition.Exists(pos => pos == new Vector2(x, z - 3))) eP += 1;
if (childrensPosition.Exists(pos => pos == new Vector2(x - 3, z + 3))) eP += 1;
if (childrensPosition.Exists(pos => pos == new Vector2(x - 3, z))) eP += 1;
if (childrensPosition.Exists(pos => pos == new Vector2(x - 3, z - 3))) eP += 1;
if (eP == 8) return true;
return false;
}
对于边缘物体来说,找到并返回可以使用的坐标位置:
private Vector2 ActiveGameObject(GameObject go)
{
int x = (int)go.transform.position.x;
int z = (int)go.transform.position.z;
if (!childrensPosition.Exists(pos => pos == new Vector2(x + 3, z + 3)))
{
return new Vector2(x +3, z + 3);
}
if (!childrensPosition.Exists(pos => pos == new Vector2(x + 3, z)))
{
return new Vector2(x + 3, z);
}
if (!childrensPosition.Exists(pos => pos == new Vector2(x + 3, z - 3)))
{
return new Vector2(x + 3, z - 3);
}
if (!childrensPosition.Exists(pos => pos == new Vector2(x, z + 3)))
{
return new Vector2(x, z + 3);
}
if (!childrensPosition.Exists(pos => pos == new Vector2(x, z - 3)))
{
return new Vector2(x, z - 3);
}
if (!childrensPosition.Exists(pos => pos == new Vector2(x - 3, z + 3)))
{
return new Vector2(x - 3, z + 3);
}
if (!childrensPosition.Exists(pos => pos == new Vector2(x - 3, z)))
{
return new Vector2(x - 3, z);
}
if (!childrensPosition.Exists(pos => pos == new Vector2(x - 3, z - 3)))
{
return new Vector2(x - 3, z - 3);
}
Debug.Log("错误,找不到空余坐标");
return Vector2.zero;
}
总体来说,第二个脚本的思路为,计算玩家与物块的距离决定是否关闭物体并加入对象池,同时把寻找离玩家最近的边缘物体(为什么要这样做?因为我们要保证场景中地面的总面积不会减少,而且非边缘物块没有可以放置的区域,故需要寻找边缘物体,为什么要找最近的?因为那是最需要的,再不填充玩家就掉下去了),寻找边缘物体8邻域中空闲的位置,在这个位置放置进入对象池的方块。至于最后的Debug,原则上是不会执行的,因为我们已经排除了非边缘情况,边缘的8邻域一定是有空闲位置的。这样我们就实现了物块的更新。
总体代码简单,只是需要一些数学逻辑,拜拜。