28-06-2021, 05:36 PM
(28-06-2021, 09:35 AM)josemendez Wrote: 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.Thank you so much i will be try this as soon as possible
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();
}
}