29-04-2025, 04:09 PM
(This post was last modified: 29-04-2025, 04:17 PM by josemendez.)
(29-04-2025, 02:14 PM)0hsyn1 Wrote: This is what I want "The onContactEnter event in ObiContactEventDispatcher is triggered when a particle belonging to the actor first touches a collider." and using the rope as a path from that point to the end.
I tested this code (which is what your original code does I think, plus the corrections added throughout this thread) and it works correctly for me, or at least the way I think you want it to work:
Code:
using UnityEngine;
using Obi;
public class RopeContactNormalized : MonoBehaviour
{
public ObiRope rope;
// Start is called before the first frame update
void Start()
{
GetComponent<ObiContactEventDispatcher>().onContactEnter.AddListener(OnRopeContactEnter);
}
public void OnRopeContactEnter(ObiSolver solver, Oni.Contact contact)
{
var world = ObiColliderWorld.GetInstance();
float normalizedCoordinate = -1;
int particleIndex = solver.simplices[contact.bodyA];
var col = world.colliderHandles[contact.bodyB].owner;
if (rope != null)
{
var elements = rope.elements;
int elementIndex = -1;
for (int i = 0; i < elements.Count; ++i)
{
var element = elements[i];
if (element.particle1 == particleIndex || element.particle2 == particleIndex)
{
elementIndex = i;
break;
}
}
if (elementIndex != -1)
{
normalizedCoordinate = (float)elementIndex / elements.Count;
Debug.Log("Normalized rope position: " + normalizedCoordinate);
}
else
{
Debug.LogWarning("Element not found for particle " + particleIndex);
}
}
else
{
Debug.LogWarning("Rope reference is missing!");
}
if (normalizedCoordinate >= 0)
{
var section = rope.GetComponent<ObiPathSmoother>().GetSectionAt(normalizedCoordinate);
Debug.DrawRay(section.position, section.normal, Color.red,10);
}
}
}