【发布时间】:2021-12-17 05:58:54
【问题描述】:
所以我正在制作一个你在海盗船上航行的游戏,我在搅拌机中为海洋制作动画并制作了一个脚本来更新网格碰撞器,以便光线投射在动画网格上正确触发,到目前为止一切都很好。
现在我面临的问题是船的问题,到目前为止,我已经能够让船乘风破浪:
// 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# unity3d rotation mesh normals