0

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

Increasingly Idiotic
  • 5,700
  • 5
  • 35
  • 73

1 Answers1

2

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;
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thanks for the help! This is definitely a step in the right direction, now I'm writing over the right info. But this still doesn't seem to do it - perhaps my issue is something to do with the number in each array I'm assigning? Should I be using [1] instead of [0]? – Julian Rojas May 01 '18 at 20:12
  • What's in this answer should fill the First index SphereCollider in the Cloth. See [this](https://i.imgur.com/AgOAKy7.png) for what it should look like. Isn't that what you are trying to do? Notice that the First one is filled. If you are not getting something similar, copy and paste the code directly from my answer since you are not typing it right – Programmer May 01 '18 at 20:18
  • Yeah, that worked! I must have typed something wrong when I implemented your solution the first time. Thank you so much!!! – Julian Rojas May 01 '18 at 21:12
  • Don't forget to [accept](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) this answer if this solved your problem. – Programmer May 02 '18 at 12:30