Obi Official Forum
Check if whole rope is inside a trigger - Printable Version

+- Obi Official Forum (https://obi.virtualmethodstudio.com/forum)
+-- Forum: Obi Users Category (https://obi.virtualmethodstudio.com/forum/forum-1.html)
+--- Forum: Obi Rope (https://obi.virtualmethodstudio.com/forum/forum-4.html)
+--- Thread: Check if whole rope is inside a trigger (/thread-2395.html)



Check if whole rope is inside a trigger - Spectra_7 - 31-07-2020

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.


RE: Check if whole rope is inside a trigger - josemendez - 31-07-2020

(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/tutorials/scriptingcollisions.html


RE: Check if whole rope is inside a trigger - Spectra_7 - 31-07-2020

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.