c# - 试图将我的船旋转到平行于网格的问题,正常它漂浮在上面
问题描述
所以我正在制作一个你在海盗船上航行的游戏,我在搅拌机中为海洋制作动画,并制作了一个脚本来更新网格对撞机,以便光线投射在动画网格上正确触发,到目前为止一切都很好。
现在我面临的问题是船,到目前为止,我已经能够让船乘风破浪:
// 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 )旋转的尝试对我来说都没有成功。
我做了一些尝试,对我来说有些效果,但其中最大的问题是由大三角形组成的网格不会给我一个平滑的旋转,而是不断地捕捉到法线,这当然不好。
所以我想要达到的最终结果是我的船会旋转,这样它就可以在视觉上上下波浪,而不是让前部穿过。
我很感激任何帮助!我一直在努力想出一个比我承认的更长的解决方案。:-)
解决方案
maxRadiansDelta
该方法中的第 4 个参数public static Vector3 RotateTowards(Vector3 current, Vector3 target, float maxRadiansDelta, float maxMagnitudeDelta);
应该允许您在命中三角形法线的方向上旋转船,并使用maxRadiansDelta
您可以设置的最大值来设置并使旋转足够平滑。
推荐阅读
- java - 在eclipselink / JPA中使用唯一约束时如何避免在一对多关系中重复插入“许多”实体
- javascript - 使用 p5.js 自动滚动到我的播放器,但它不起作用
- powerbi - POWER BI 可视化 - 具有不同状态的多个列
- javascript - 模板中的 href 和 Url 在 Django 中不起作用
- html - Go 中我的应用程序结构存在问题
- python - 在数据框中添加批次的总和
- r - 如何在我的函数中使用 2 个不同的 y 变量在同一页面上创建 2 个单独的图
- iis - 由 IIS 托管时数据库连接失败
- wordpress - (Elementor) 我可以添加与滑块重叠的小动画 gif 吗?
- javascript - 反应路由器路径:在路径内制作可选值