17-11-2021, 05:36 PM
(This post was last modified: 17-11-2021, 05:40 PM by greyhawk.
Edit Reason: Subscribe and receive email notification of new replies
)
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
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();
}
}
}