Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check if whole rope is inside a trigger
#1
Hello. First of all, great asset.

The rope falls down from top.
I want to trigger an event when the whole rope is inside this box trigger. Is this possible?
[Image: k3ZkSx7.png]

Thanks.
Reply
#2
(31-07-2020, 12:55 PM)Spectra_7 Wrote: Hello. first of all, great asset.

The rope falls down from top.
I want to trigger an event when the whole rope is inside this box trigger. Is this possible?
[Image: k3ZkSx7.png]

Thanks.

Sure,

Just subscribe to the solver's OnCollision event. If all particles belonging to the rope have a contact with the trigger, you call whatever function you need. See:
http://obi.virtualmethodstudio.com/tutor...sions.html
Reply
#3
Thanks for the reply.

Like you said, I got it work the way I wanted. I am dropping the script here in case someone finds it useful:
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;

public class RopeTriggerCheck : MonoBehaviour
{
    public ObiSolver solver;
    public ObiActor ropeActor;
    public Collider triggerCollider;
    public float currentParticlesCount = 0;
    public float totalParticlesCount;
    public bool isEventTriggered;

    public List<int> particlesEnteredList = new List<int>();

    private ObiRopeBlueprint ropeBlueprint;
   
    private void Start()
    {
        if(ropeActor == null)
            ropeActor = solver.actors[0];

        ropeBlueprint = (ObiRopeBlueprint)ropeActor.blueprint;
        totalParticlesCount = ropeBlueprint.activeParticleCount;
        isEventTriggered = false;
    }

    private void OnEnable()
    {
        solver.OnCollision += Solver_OnCollision;
    }

    private void OnDisable()
    {
        solver.OnCollision -= Solver_OnCollision;
    }

    void Solver_OnCollision(object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
    {
        //print(e.contacts.Count);       

        for (int i = 0; i < e.contacts.Count; ++i)
        {
            if (e.contacts.Data[i].distance < 0.01f)
            {
                Component collider;
                if (ObiCollider.idToCollider.TryGetValue(e.contacts.Data[i].other, out collider))
                {
                    if (collider == triggerCollider)
                    {
                        if(!particlesEnteredList.Contains(e.contacts.Data[i].particle))
                            particlesEnteredList.Add(e.contacts.Data[i].particle);

                        currentParticlesCount = particlesEnteredList.Count;

                        if(currentParticlesCount >= totalParticlesCount && !isEventTriggered)
                        {
                            //Trigger an Event you like
                            isEventTriggered = true;                           
                           
                        }
                    }
                }
            }
        }
    }
}

Thanks a lot.
Reply