- Home /
Set Unity 5 cloth constraints from code?
Performance on the new cloth is great, but I've got a huge amount of points on my mesh to sort through, some constrained, some not. If I could set it by code it would save me hours of pain.
I can't find anywhere in the documentation whether it's possible to set the constraints from a script, any ideas?
I would also love this feature! I know that the standard parameters of the Cloth Component (Stretching Stiffness, Bending Stiffness, Solver frequency, etc) is available from code but we need access to the per-vertex "$$anonymous$$ax Distance" and "Surface Penetration Values" shown in this image:
Unity 5 Cloth Documentation will need some updating as well!
Answer by smallbit · Sep 27, 2015 at 05:59 PM
I know its late, but maybe someone else with stump upon this; So to edit the constraints from code (maxDistance), you use "coefficients" field from your cloth object which is an array of type ClothSkinningCoefficient and size of the number of verts in your mesh you attached cloth to. You can edit max distance for each vertex on the runtime. However the only way you can get is work is to replace the entire array of cloth.coefficients, editing a simple value there (i.e. cloth.coefficients[18].maxDistance = 0.5f will not have any effect on the cloth). So here is the code you would need:
ClothSkinningCoefficient[] newConstraints;
newConstraints = cloth.coefficients;
newConstraints[5].maxDistance = 2.5f;
cloth.coefficients = newConstraints;
Answer by xmanreturn · Feb 14, 2018 at 04:20 AM
Same question here, I need to manually set the vertices in the constraints, e.g. I have a procedural generated mesh, I need to attach cloth component to the mesh, thus the only way to set vertices is from code. but you can only set maxdistance via ClothSkinningCoefficient. What Can I do?? Please help @smallbit
Your answer
![](https://koobas.hobune.stream/wayback/20220613195551im_/https://answers.unity.com/themes/thub/images/avi.jpg)