Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Oncollision detection and cut rope problem
#1
Pregunta 
Greetings, I am working with OnCollision on a  mechanic that will cut the  rope as a result of contact with objects such as saws, knives and similar objects that can cut the rope.

In addition,3 ropes can interact physically with each other. Trying to cut one, the other is cut. Each one has its own blueprint.

How can I fix the bug in the code I wrote?

You can see the script I wrote
Code:
using System.Collections.Generic;
using UnityEngine;
using Obi;

namespace _Game.Scripts.Entities.Rope
{
    [RequireComponent(typeof(ObiSolver))]
    public class RopeCutter : MonoBehaviour
    {
        ObiSolver solver;
        public List<ObiStructuralElement> ropeElements;
       
        void Awake(){
            solver = GetComponent<Obi.ObiSolver>();
        }

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

        void OnDisable(){
            solver.OnCollision -= Solver_OnCollision;
        }
        void Solver_OnCollision(object sender, ObiSolver.ObiCollisionEventArgs e)
        {
            var world = ObiColliderWorld.GetInstance();

            // just iterate over all contacts in the current frame:
            foreach (Oni.Contact contact in e.contacts)
            {
                // if this one is an actual collision:
                if (contact.distance < 0.01);
                {
                    ObiColliderBase col = world.colliderHandles[contact.bodyB].owner;
                    if(col !=null)
                    TearRope(col, contact);
                }
               
            }
        }

        private void TearRope(ObiColliderBase col, Oni.Contact contact)
        {
            if (!col || !col.CompareTag("saw")) return;
            //int particleIndex = solver.simplices[contact.bodyA];
            ObiSolver.ParticleInActor pa = solver.particleToActor[contact.bodyA];
            if (pa == null) return;

            var actor = pa.actor as ObiRope;
            if (!actor) return;
      
            if (pa.indexInActor >= actor.elements.Count) return;

            actor.Tear(actor.elements[pa.indexInActor]);
            actor.RebuildConstraintsFromElements();
        }
       
    }
}


Attached Files Thumbnail(s)
   

.png   Resim2.png (Size: 5.05 KB / Downloads: 41)
Reply


Messages In This Thread
Oncollision detection and cut rope problem - by greyhawk - 17-11-2021, 05:36 PM