- Home /
How do I Raycast a Cloth? Or use OnMouseDown?
I've been trying to use a raycast from the camera to the world, which has worked so far on all objects except for Interactive Cloth. I am aiming to, upon clicking on the cloth object, bring it up to the camera - which I have been able to do for all or objects except for my cloth physics-based objects.
Is there a way to pick up said clothes with ray casting, or detect them with OnMouseDown? Or perhaps a substitute for
Answer by DRProductions · Jan 18, 2014 at 08:17 PM
Are you sure the cloth has a collider? Try adding a box collider to the cloth object this may solve your issue.
The cloth does collide with the world, however it does not interact with rays. wouldn't adding a box collider would surely make the cloth glitchy and unrealistic in motion though? Or should it be a trigger?
you could make the box collider on another object around the cloth and set the cloth and collider on different layers and the go to Edit>Project Setting>Physics> and then go to the Layer Collision matrix to not colide those layers together
Thanks! is there a non cpu-intensive way to create a new collision mesh based on the cloth shape every update though? Just wondering if that's possible
I am not sure. but on a side note if my answer helped you could you please mark it as a good answer?
Answer by Ryuuguu · Apr 03, 2014 at 01:53 PM
I do not know if this nonCPU intensive or not but it works. Make a separate game object with a mesh collider as child at (0,0,0) position relative to the cloth. put in in a lyer that does collide with the cloth. This gives a mesh collider that follows the cloth.
put this on the object
public InteractiveCloth cloth;
public Mesh mesh;
void Start () {
mesh = (Mesh)Instantiate(cloth.mesh);
GetComponent<MeshCollider>().sharedMesh = mesh;
}
void Update () {
calcFreqCount = calcFreq;
GetComponent<MeshCollider>().sharedMesh = null;
mesh.vertices = cloth.vertices;
GetComponent<MeshCollider>().sharedMesh = mesh;
}