Obi Official Forum
Help Obi tearable cloth - 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 Obi tearable cloth (/thread-1139.html)



Obi tearable cloth - Richard - 30-04-2019

I asked almost same question, but I could not get sign of when it is torn. I want an example to achieve.


RE: Obi tearable cloth - josemendez - 01-05-2019

(30-04-2019, 03:52 PM)Richard Wrote: I asked almost same question, but I could not get sign of when it is torn. I want an example to achieve.

Hi,

You already asked this same question, and a complete example was provided. See:
http://obi.virtualmethodstudio.com/forum/showthread.php?tid=960&highlight=callback

This is the sample code. Simply subscribe a function to the cloth's OnConstraintTorn callback. That's all there is to it:

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: Obi tearable cloth - Richard - 01-05-2019

(01-05-2019, 12:34 AM)josemendez Wrote: Hi,

You already asked this same question, and a complete example was provided. See:
http://obi.virtualmethodstudio.com/forum/showthread.php?tid=960&highlight=callback

This is the sample code. Simply subscribe a function to the cloth's OnConstraintTorn callback. That's all there is to it:

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);
   }
}

How can I callback with that?


RE: Obi tearable cloth - josemendez - 01-05-2019

(01-05-2019, 01:37 AM)Richard Wrote: How can I callback with that?

This is a regular C# event. Please read on how to use them.

https://www.tutorialspoint.com/csharp/csharp_events.htm

https://www.tutorialsteacher.com/csharp/csharp-event


RE: Obi tearable cloth - Richard - 01-05-2019

(01-05-2019, 09:50 AM)josemendez Wrote: This is a regular C# event. Please read on how to use them.

https://www.tutorialspoint.com/csharp/csharp_events.htm

https://www.tutorialsteacher.com/csharp/csharp-event

I used code like this
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;

[RequireComponent(typeof(ObiTearableCloth))]
public class ClothTornEvent : MonoBehaviour
{
   void Start()
   {
       this.GetComponent<ClothTornEvent>().enabled = false;
   }

   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)
   {
       Debug.Log("1");
       if (thingToSpawn != null)
       {
           GameObject.Instantiate(thingToSpawn, cloth.Solver.positions[cloth.particleIndices[e.particleIndex]], Quaternion.identity);
           Debug.Log("2");
       }
           
   }
}
However, I could not see "1" or "2" when the cloth is tore.  I need another thing? How can I get input from obi tearable cloth?


RE: Obi tearable cloth - josemendez - 01-05-2019

(01-05-2019, 11:42 AM)Richard Wrote: I used code like this
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;

[RequireComponent(typeof(ObiTearableCloth))]
public class ClothTornEvent : MonoBehaviour
{
   void Start()
   {
       this.GetComponent<ClothTornEvent>().enabled = false;
   }

   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)
   {
       Debug.Log("1");
       if (thingToSpawn != null)
       {
           GameObject.Instantiate(thingToSpawn, cloth.Solver.positions[cloth.particleIndices[e.particleIndex]], Quaternion.identity);
           Debug.Log("2");
       }
           
   }
}
However, I could not see "1" or "2" when the cloth is tore.  I need another thing? How can I get input from obi tearable cloth?

Maybe not disabling your component immediately in Start() would work Guiño


RE: Obi tearable cloth - Richard - 01-05-2019

(01-05-2019, 11:52 AM)josemendez Wrote: Maybe not disabling your component immediately in Start() would work Guiño

I wrote the code because of another problem, but it is okay now. Sorry.