- Home /
Can add a constraint to a cloth at runtime, but can't remove one.
My code starts with one constraint on the cloth, defined in the editor. My goal is to remove that constraint and add another constraint at a different point, both in code.
ClothSkinningCoefficient[] coefficientsCopy = cloth.coefficients;
coefficientsCopy[vertexToConstrain].maxDistance = 0;
// An unconstrained vertex seems to be listed as float.MaxValue in code
coefficientsCopy[vertexToUnconstrain].maxDistance = float.MaxValue;
cloth.coefficients = coefficientsCopy;
When I check cloth.coefficients[vertexToUnconstrain] after this code is executed, it's set to float.MaxValue, and if I pause the editor and check the constraints under "Edit Constraints", the vertex at vertexToConstrain is marked as constrained and vertexToUnconstrain is marked as unconstrained, but vertexToUnconstrain still behaves as though it is constrained in-editor.
To illustrate: Here is the cloth before I run the simulation. The only constrained vertex is in the upper center (which will be VertexToUnconstrain when I run the simulation).
Here is the cloth after I run the simulation. I've selected the vertex in the upper left corner as vertexToConstrain, and vertexToUnconstrain (upper right corner) is no longer marked as constrained. However, it still behaves as though it were constrained.
Thanks for your help!
Answer by anselmkegel · Jun 12, 2018 at 05:00 PM
I had the same problem and fould a way to get it working. The code above is correct so far, but you need to re-enable the cloth component. Apparently the setup code happens in OnEnable(). So just disable and enable it again and it works.
ClothSkinningCoefficient[] coefficients = cloth.coefficients;
for (int i = 0, iend = coefficients.Length; i < iend ; ++i)
{
coefficients[i].maxDistance = float.MaxValue;
}
cloth.coefficients = coefficients;
cloth.enabled = false;
cloth.enabled = true;
It actually doesn't matter when you disable the component. You can do it before copying the coefficients or just right before re-enabling it. You just need to make sure to re-enable it after copying the coefficients.
Your answer
Follow this Question
Related Questions
[Cloth Physics]How is surface penetration achieved in Unity ? 0 Answers
Can you apply physics to only part of an object? 2 Answers
Excluding Vertices from Cloth Colliders in Unity 5 4 Answers
How to walk around a 3D sphere? 0 Answers
Softbodies in Unity 3D 1 Answer