Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  get sign when cloth is tore
#1
Hello.

I want to make if statement when the cloth is tore. How can I do that?
Reply
#2
(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);
    }
}


Attached Files
.cs   ObiTearableCloth.cs (Size: 25.02 KB / Downloads: 4)
Reply
#3
(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?
Reply
#4
(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.


Attached Files
.cs   ObiTearableCloth.cs (Size: 29.91 KB / Downloads: 7)
Reply
#5
(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'
Reply
#6
(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.
Reply
#7
(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.
Reply
#8
(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
Reply
#9
(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.


Attached Files Thumbnail(s)
   
Reply
#10
(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
Reply