It’s actually a random speed that you want, not a random velocity (‘velocity’ includes both speed and direction, and the direction is already determined by the normal of the polygon).
I believe GetTriangles() returns the triangles for a submesh. To access all the triangles in the mesh, just use the ‘triangles’ property/member variable.
Actually it would be vertices[triangles[0]], vertices[triangles[1]], vertices[triangles[2]].
I’m fairly certain that Unity converts all polygons with more than 3 sides into triangles on import.
As for ‘exploding a mesh’, I haven’t actually done this in Unity, but I’ve written similar code in other projects. Here’s how I would most likely proceed:
-
For each triangle in the mesh…
-
Create a new game object
-
Create a mesh for the game object (you’ll need a filter and a renderer)
-
Assign the mesh data for the triangle in question to the new mesh
-
Compute the face direction as the cross product of two of its edges (there are normals stored with the mesh, but they’re vertex normals rather than face normals)
-
Assign the new object an initial velocity of normal * initialSpeed
Note that this will break coplanar triangles apart (that is, faces that had more than 3 vertices in the original mesh will not break apart in one piece). If you want to keep the original faces together, you’ll need to preprocess the mesh to identify coplanar triangles, and then keep these together when exploding the mesh.
Another issue is that the newly created triangles will most likely not be centered at the origin, which may or may not cause problems, depending on how you want the spawned pieces to behave. To be safe though, I would probably translate each triangle to the origin, and then position the spawned game object at the triangle’s original world-space center.
Also keep in mind that you’ll need to transform the mesh vertices into world space as you go, using the transform of the exploding object.
Now, this was very cursory - there are a lot of details that I glossed over. It’s not trivial, but it should be do-able with some effort.
If you get stuck on anything specific, post back here and I or someone else will probably be able to help.