Unity3D: Adding Sphere Collider to Cloth Component at Runtime Isn't Doing Anything

1.3k views Asked by At

I'm trying to have it so that the player walks through a cloth curtain.

There's a sphere collider attached to my first person camera, and a cloth script attached to a prim plane. I'm trying to attach the FPS cam collider to the cloth object at runtime.

I wrote a script that seems like it should work, but it doesn't. No errors or nothing. The script compiles and works, but the sphere collider just doesn't connect to the cloth component. what gives?

    public class ClothTest : MonoBehaviour {

          private void Start() {

    Cloth a = GetComponent<Cloth>();

    var ClothColliders = new ClothSphereColliderPair[1];
    ClothColliders[0] = new ClothSphereColliderPair(GameObject.Find("First Person Camera").GetComponent<SphereCollider>());

    ClothColliders[0] = a.sphereColliders[0];

}

Here is a screenshot of the cloth component in the inspector:

Here is a screenshot of the cloth component in the inspector

1

There are 1 answers

4
Programmer On

You are trying to put the SphereCollider from your camera to your Cloth but

ClothColliders[0] = a.sphereColliders[0]; 

is doing the opposite. It is trying to put the SphereCollider from your Cloth to the one in your camera. Change that around and also remove the [0] from each side.

That last line of code should be:

a.sphereColliders = ClothColliders;

The complete new function:

void Start()
{
    Cloth a = GetComponent<Cloth>();
    var ClothColliders = new ClothSphereColliderPair[1];
    ClothColliders[0] = new ClothSphereColliderPair(GameObject.Find("First Person Camera").GetComponent<SphereCollider>());

    a.sphereColliders = ClothColliders;
}