Obi Official Forum
Help get sign when cloth is tore - Printable Version

+- Obi Official Forum (https://obi.virtualmethodstudio.com/forum)
+-- Forum: Obi Users Category (https://obi.virtualmethodstudio.com/forum/forum-1.html)
+--- Forum: Obi Cloth (https://obi.virtualmethodstudio.com/forum/forum-2.html)
+--- Thread: Help get sign when cloth is tore (/thread-960.html)

Pages: 1 2


get sign when cloth is tore - Richard - 07-03-2019

Hello.

I want to make if statement when the cloth is tore. How can I do that?


RE: get sign when cloth is tore - josemendez - 07-03-2019

(07-03-2019, 10:20 AM)Richard Wrote: Hello.

I want to make if statement when the cloth is tore. How can I do that?

Hi,

You could constantly poll for a difference in used particles, but that's not very efficient. Using an event is much better.

I've modified ObiTearableCloth.cs to include an event callback when the cloth is torn, please find it attached. This event is passed the constraint and particle indices involved in the tear event by parameter.

Here's a usage example: this script subscribes to the callback event, and spawns a GameObject at the position of each torn particle.

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;

[RequireComponent(typeof(ObiTearableCloth))]
public class ClothTornEvent : MonoBehaviour {

    ObiTearableCloth cloth;
    public GameObject thingToSpawn;

    void OnEnable () {
        cloth = GetComponent<ObiTearableCloth>();
        cloth.OnConstraintTorn += Cloth_OnConstraintTorn;
    }
    
    void OnDisable(){
        cloth.OnConstraintTorn -= Cloth_OnConstraintTorn;
    }
    
    void Cloth_OnConstraintTorn (object sender, ObiTearableCloth.ObiConstraintTornEventArgs e)
    {
        if (thingToSpawn != null)
            GameObject.Instantiate(thingToSpawn,cloth.Solver.positions[cloth.particleIndices[e.particleIndex]],Quaternion.identity);
    }
}



RE: get sign when cloth is tore - Richard - 07-03-2019

(07-03-2019, 11:05 AM)josemendez Wrote: Hi,

You could constantly poll for a difference in used particles, but that's not very efficient. Using an event is much better.

I've modified ObiTearableCloth.cs to include an event callback when the cloth is torn, please find it attached. This event is passed the constraint and particle indices involved in the tear event by parameter.

Here's a usage example: this script subscribes to the callback event, and spawns a GameObject at the position of each torn particle.

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;

[RequireComponent(typeof(ObiTearableCloth))]
public class ClothTornEvent : MonoBehaviour {

    ObiTearableCloth cloth;
    public GameObject thingToSpawn;

    void OnEnable () {
        cloth = GetComponent<ObiTearableCloth>();
        cloth.OnConstraintTorn += Cloth_OnConstraintTorn;
    }
    
    void OnDisable(){
        cloth.OnConstraintTorn -= Cloth_OnConstraintTorn;
    }
    
    void Cloth_OnConstraintTorn (object sender, ObiTearableCloth.ObiConstraintTornEventArgs e)
    {
        if (thingToSpawn != null)
            GameObject.Instantiate(thingToSpawn,cloth.Solver.positions[cloth.particleIndices[e.particleIndex]],Quaternion.identity);
    }
}

An error is displayed when I download the code.

Assets\ObiTearableCloth.cs(81,23): error CS0115: 'ObiTearableCloth.OnSolverStepEnd(float)': no suitable method found to override

How can I fix this?


RE: get sign when cloth is tore - josemendez - 07-03-2019

(07-03-2019, 12:27 PM)Richard Wrote: An error is displayed when I download the code.

Assets\ObiTearableCloth.cs(81,23): error CS0115: 'ObiTearableCloth.OnSolverStepEnd(float)': no suitable method found to override

How can I fix this?

Sorry, I wrote this modification on top of v4.0.3. For earlier versions simply remove the float argument from the function definition on ObiTearableCloth.cs. I attached the new file with this slight change.


RE: get sign when cloth is tore - Richard - 07-03-2019

(07-03-2019, 12:39 PM)josemendez Wrote: Sorry, I wrote this modification on top of v4.0.3. For earlier versions simply remove the float argument from the function definition on ObiTearableCloth.cs. I attached the new file with this slight change.

New error happened... 

Assets\Obi\Editor\ObiTearableClothEditor.cs(28,23): error CS0433: The type 'ObiTearableCloth' exists in both 'Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' and 'Obi, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'

Assets\Obi\Editor\ObiTearableClothEditor.cs(61,3): error CS0433: The type 'ObiTearableCloth' exists in both 'Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' and 'Obi, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'


RE: get sign when cloth is tore - josemendez - 07-03-2019

(07-03-2019, 01:10 PM)Richard Wrote: New error happened... 

Assets\Obi\Editor\ObiTearableClothEditor.cs(28,23): error CS0433: The type 'ObiTearableCloth' exists in both 'Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' and 'Obi, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'

Assets\Obi\Editor\ObiTearableClothEditor.cs(61,3): error CS0433: The type 'ObiTearableCloth' exists in both 'Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' and 'Obi, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'

That means you haven't replaced the existing file, but duplicated it. Please replace the existing ObiTearableCloth.cs file with the provided one.


RE: get sign when cloth is tore - Richard - 07-03-2019

(07-03-2019, 01:48 PM)josemendez Wrote: That means you haven't replaced the existing file, but duplicated it. Please replace the existing ObiTearableCloth.cs file with the provided one.

The ObiTearalbleCltoth.cs file is red icon? I replaced it and erased the error.


RE: get sign when cloth is tore - josemendez - 07-03-2019

(07-03-2019, 02:11 PM)Richard Wrote: The ObiTearalbleCltoth.cs file is red icon? I replaced it and erased the error.

Yes, its icon is an orange flag. It is found in Obi/Scripts/Actors/ObiTearableCloth.cs


RE: get sign when cloth is tore - Richard - 07-03-2019

(07-03-2019, 02:13 PM)josemendez Wrote: Yes, its icon is an orange flag. It is found in Obi/Scripts/Actors/ObiTearableCloth.cs

I cannot find out what is wrong, but here is an error.


RE: get sign when cloth is tore - josemendez - 07-03-2019

(07-03-2019, 02:34 PM)Richard Wrote: I cannot find out what is wrong, but here is an error.

There should not be a file named "ObiTearableCloth (1)".

It should be "ObiTearableCloth". Again, look trough your project for copies of this file and remove them, since the name "ObiTearableCloth (1)" indicates you have created a copy of the file, instead of replacing the original one.

There should be only one file named ObiTearableCloth.cs, located at Obi/Scripts/Actors/ObiTearableCloth.cs