public float height = 0.05f;
public LayerMask TargetLayer;
[HideInInspector]
public bool isNeedAdaptSurfaces = true;
[HideInInspector]
public bool isFly = false;
private void Update()
{
AdaptSurfaces();
}
/// <summary>
/// 匹配曲面
/// </summary>
public void AdaptSurfaces()
{
Ray ray = new Ray(transform.position + Vector3.up * 0.1f, Vector3.down);
RaycastHit hit;
Debug.DrawLine(transform.position + Vector3.up * 0.1f, Vector3.down);
// 检测射线与模型的碰撞
if (Physics.Raycast(ray, out hit, 100, TargetLayer))
{
// 获取碰撞点的法线向量
if (isNeedAdaptSurfaces)
transform.up = hit.normal;
if (!isFly)
transform.position = hit.point + Vector3.up * height;
}
}
unity移动匹配曲面
最新推荐文章于 2024-11-02 14:12:37 发布