首页 > 解决方案 > 试图将我的船旋转到平行于网格的问题,正常它漂浮在上面

问题描述

所以我正在制作一个你在海盗船上航行的游戏,我在搅拌机中为海洋制作动画,并制作了一个脚本来更新网格对撞机,以便光线投射在动画网格上正确触发,到目前为止一切都很好。

现在我面临的问题是船,到目前为止,我已经能够让船乘风破浪:

// This is the script that handles the riding of the waves.

[SerializeField] private LayerMask m_whatIsSea;
[SerializeField] private float m_offsetFromWaterSurface;

private void Update()
{
    // Shoot a raycast from above the water down to detect the height.
    RaycastHit _hit;
    if (Physics.Raycast(gameObject.transform.position + (Vector3.up * (Mathf.Abs(m_offsetFromWaterSurface * 4))), -Vector3.up, out _hit, m_whatIsSea))
    {
        Float(_hit);
    }
}

private void Float(RaycastHit hit)
{
    // Than move the boat to the height.
    Vector3 _newPosition = new Vector3(gameObject.transform.position.x, hit.point.y + m_offsetFromWaterSurface, gameObject.transform.position.z);
    transform.position = _newPosition;
}

但是当我用船控制器移动船时,它确实正确地改变了它的高度。但是所有使我的船根据光线投射网格法线(上面代码中的 hit.normal )旋转的尝试对我来说都没有成功。

我做了一些尝试,对我来说有些效果,但其中最大的问题是由大三角形组成的网格不会给我一个平滑的旋转,而是不断地捕捉到法线,这当然不好。

波浪网的图片:

所以我想要达到的最终结果是我的船会旋转,这样它就可以在视觉上上下波浪,而不是让前部穿过。

我很感激任何帮助!我一直在努力想出一个比我承认的更长的解决方案。:-)

标签: c#unity3drotationmeshnormals

解决方案


我会尝试Vector3.RotateTowards

maxRadiansDelta该方法中的第 4 个参数public static Vector3 RotateTowards(Vector3 current, Vector3 target, float maxRadiansDelta, float maxMagnitudeDelta);应该允许您在命中三角形法线的方向上旋转船,并使用maxRadiansDelta您可以设置的最大值来设置并使旋转足够平滑。


推荐阅读