I have been searching around everywhere for how to procedurally generate a capsule mesh in code. I need to procedurally generate a capsule mesh because I want to be able to control its vertices at runtime. Is there any way around this?
Asked
Active
Viewed 462 times
-1
-
What do you mean by procedurally generate? Do you want to just add a collider at runtime then set the public fields? `gameObject.AddComponent
();` and store the reference you create. Then just set whatever data you want. – TEEBQNE May 16 '21 at 20:56 -
1If you want to fully just generate a capsule and not a collider, then you can look at [this thread](https://forum.unity.com/threads/solved-closed-procedurally-generated-capsule.406982/). – TEEBQNE May 16 '21 at 21:05
-
[Making a Capsule Mesh via Script in Five 3D Environments](https://behreajj.medium.com/making-a-capsule-mesh-via-script-in-five-3d-environments-c2214abf02db) – derHugo May 17 '21 at 03:54
-
Look for guides on creating a sphere mesh, like [this](https://stackoverflow.com/questions/4081898/procedurally-generate-a-sphere-mesh) and simply add an offset on the top half of the vertices. – JonasH May 17 '21 at 08:10
1 Answers
-1
I need to procedurally generate a capsule mesh because I want to be able to control its vertices at runtime. Is there any way around this?
Consider creating a new GameObject
during runtime who's mesh is already a capsule, and modifying the vertices manually.
One way you may be able to do this is by using GameObject.CreatePrimitive
. You can use this to create a capsule at runtime, who's mesh you can alter.
GameObject capsule = GameObject.CreatePrimitive(PrimitiveType.Capsule);
Once you have the capsule created at runtime you can access it's mesh using the Mesh
class.
For example if you wanted to scale a box two times it's size using it's vertices you could do something like:
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
for (int i = 0; i < vertices.Length; i++)
{
vertices[i] = vertices[i] * 2;
}
mesh.SetVertices(vertices);
mesh.RecalculateBounds();
mesh.RecalculateNormals();
mesh.RecalculateTangents();

DekuDesu
- 2,224
- 1
- 5
- 19
-
Doesn't help OP much to controle the verts at runtime .. e.g. for knowing which is the top and bottom half etc If you suggest to use the built-in primitive .. why not directly create the capsule during edit mode anyway? – derHugo May 17 '21 at 03:56
-
Hi thanks for the response! According to OP "I need to procedurally generate a capsule mesh **because I want to be able to control its vertices at runtime**". Using this information we can derive that OP will at some point will need to control the verts. With the use of a primitive as a starting point, instead of creating one from scratch at run-time, it may reduce their code's complexity significantly, as normals, triangles, and object references are already resolved during instantiation. OP specifically states he wants to control the verts, so I provided one way he might accomplish his task. – DekuDesu May 17 '21 at 04:52
-
What mean is with this argumentation is .. well, yes of course there is `Mesh.vertices` and most probably the way how OP wants to controle the vertices .. because this is how such thing is done yes ... However, the question here is not in general how to manipulate the vertices of literally any mesh but rather how to create the mesh in a way so you later can manipulate only certain vertices in a meaningful way .. for the general thing you refer to there is away more efficient solution already: `transform.localScale *= 2;` – derHugo May 17 '21 at 06:08