I use this piece of code in Unity to get the distance length of my model entering another model. I have set collision markers at the tip and end of the model and performed raycasting, but Unity currently does not support object tracking in VisionOS. Therefore, I plan to use SwiftUI for native development. In Reality Composer Pro, I haven't seen a collision editing feature like in Unity; I can only set the size of the collision body but cannot manually adjust or visualize the shape and size of my collision body.
I want to achieve similar functionality using SwiftUI, to be able to calculate and display the distance that my model A, like a needle or ruler, penetrates into another model or a physical object's interior. Is there a similar functionality available, or other coding methods to achieve this?
void CalculateLengthInsideOrgan()
{
// Direction from the base of the probe to the tip
Vector3 direction = probeTip.position - probeBase.position;
float probeLength = direction.magnitude;
// Raycasting
RaycastHit[] hits = Physics.RaycastAll(probeBase.position, direction, probeLength, organLayerMask);
if (hits.Length > 0)
{
// Calculate the length entering the organ
float distanceToFirstHit = hits[0].distance;
lengthInsideOrgan = probeLength - distanceToFirstHit;
}
else
{
lengthInsideOrgan = 0f;
}
}