本文主要是介绍Unity通过Terrain地形数据计算人物位置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
当地形高度不在0点的时候,设置人物位置会出现问题,需要计算Terrain.activeTerrain.GetPosition().y的值
float height;Vector3 newPos;float halfModelY;float terrainHeight;private void SetModelHight(){if (GetComponent<MeshFilter>() != null){halfModelY = GetComponent<MeshFilter>().sharedMesh.bounds.size.y * transform.lossyScale.y / 2;}}// Update is called once per framevoid Update(){if (canMove){transform.Translate(Vector3.forward * Time.deltaTime * speed);if (Terrain.activeTerrain != null){float height = Terrain.activeTerrain.SampleHeight(transform.position);terrainHeight = Terrain.activeTerrain.GetPosition().y;Debug.Log(height);newPos.x = transform.position.x;newPos.y = height + halfModelY+ terrainHeight;newPos.z = transform.position.z;transform.position = newPos;}}}
这篇关于Unity通过Terrain地形数据计算人物位置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!