Unity:我可以在运行时更改网格碰撞器的形状吗?

Unity : can I change the shape of a mesh collider at runtime?

我有一个网格可以根据用户的输入更新它的形状,我怎样才能让对撞机匹配网格的新形状?

重要说明:网格始终是凸面的(无一例外)。我看到对撞机正常工作很重要。

我找到了以下视频here,但通过执行 meshCollider.sharedMesh = null 然后 meshCollider.sharedMesh = updatedMesh.

可以简单地擦除并为每一帧重新计算对撞机

由于我已经在计算网格的每个顶点、边和三角形的位置,我可以将这些值用于网格碰撞器吗?

没办法做到这一点,因为对撞机没有为此的构造函数。你正在做的事情在 unity 文档中被明确反对

'You should not modify mesh geometry that is used for colliders because the physics engine has to rebuild an internal mesh collision acceleration structure every time you change the mesh. This causes a substantial performance overhead. For meshes that need to collide and change at runtime, it is often better to approximate the mesh shape with primitive colliders like capsules, spheres and boxes.'

一个解决方法是在你的方法上使用 InvokeRepeating 并每秒而不是每一帧创建一个新的网格碰撞器(你可以测试并使其成为 0.1 秒,2 秒,任何可以 运行 没有帧丢失).您可以在对象完成移动后调用 CancelInvoke。