Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Obi tearable cloth
#1
I asked almost same question, but I could not get sign of when it is torn. I want an example to achieve.
Reply
#2
(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...t=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);
   }
}
Reply
#3
(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...t=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?
Reply
#4
(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
Reply
#5
(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?
Reply
#6
(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
Reply
#7
(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.
Reply