28-06-2021, 09:35 AM
(This post was last modified: 28-06-2021, 09:35 AM by josemendez.)
Here's working code to remove part of the rope after a cut. I've subscribed to the rope's OnRopeTorn event, there I remove all elements after the cut.
The only caveat is when iterating trough elements and removing them, you must iterate backwards to avoid skipping over elements.
The only caveat is when iterating trough elements and removing them, you must iterate backwards to avoid skipping over elements.
Code:
using UnityEngine;
using Obi;
[RequireComponent(typeof(ObiRope))]
public class DestroyTorn : MonoBehaviour
{
void Start()
{
GetComponent<ObiRope>().OnRopeTorn += DestroyTeared_OnRopeTorn;
}
private void DestroyTeared_OnRopeTorn(ObiRope rope, ObiRope.ObiRopeTornEventArgs tearInfo)
{
for (int i = rope.elements.Count-1; i >= 0; --i)
{
var elm = rope.elements[i];
rope.DeactivateParticle(rope.solver.particleToActor[elm.particle2].indexInActor);
rope.elements.RemoveAt(i);
if (elm == tearInfo.element)
break;
}
rope.RebuildConstraintsFromElements();
}
}