using Unity.XR.CoreUtils;
namespace UnityEngine.XR.Interaction.Toolkit.Locomotion
{
///
/// Container for an that can be transformed using the user's body as a frame of reference.
///
///
public class XRMovableBody
{
///
/// The XR Origin whose is transformed to move the body.
///
public XROrigin xrOrigin { get; private set; }
///
/// The Transform component of the of the .
/// This is the Transform component that is manipulated to move the body.
///
public Transform originTransform => xrOrigin.Origin.transform;
///
/// The object that determines the position of the user's body.
///
public IXRBodyPositionEvaluator bodyPositionEvaluator { get; private set; }
///
/// Object that can be used to perform movement that is constrained by collision (optional, may be ).
///
public IConstrainedXRBodyManipulator constrainedManipulator { get; private set; }
///
/// Initializes a new instance of a movable body.
///
/// The XR Origin associated with the body.
/// The object that determines the position of the user's body.
public XRMovableBody(XROrigin xrOrigin, IXRBodyPositionEvaluator bodyPositionEvaluator)
{
this.xrOrigin = xrOrigin;
this.bodyPositionEvaluator = bodyPositionEvaluator;
}
///
/// Gets the position of where the user's body is grounded (e.g. their feet), in the local space of the
/// , based on the .
///
/// Returns the position of where the user's body is grounded, in the local space of the .
public Vector3 GetBodyGroundLocalPosition()
{
return bodyPositionEvaluator.GetBodyGroundLocalPosition(xrOrigin);
}
///
/// Gets the world position of where the user's body is grounded (e.g. their feet), based on the
/// .
///
/// Returns the world position of where the user's body is grounded.
public Vector3 GetBodyGroundWorldPosition()
{
return bodyPositionEvaluator.GetBodyGroundWorldPosition(xrOrigin);
}
///
/// Links the given constrained manipulator to this body. This sets to
/// and calls on the
/// manipulator.
///
/// The constrained manipulator to link.
///
/// If is already not when this is called, this
/// first calls on .
/// Also, if the given already has a
/// set, this calls on that body.
///
///
public void LinkConstrainedManipulator(IConstrainedXRBodyManipulator manipulator)
{
constrainedManipulator?.OnUnlinkedFromBody();
manipulator.linkedBody?.UnlinkConstrainedManipulator();
constrainedManipulator = manipulator;
constrainedManipulator.OnLinkedToBody(this);
}
///
/// Unlinks the assigned constrained manipulator from this body, if there is one. This calls
/// on the manipulator and sets
/// to .
///
///
public void UnlinkConstrainedManipulator()
{
constrainedManipulator?.OnUnlinkedFromBody();
constrainedManipulator = null;
}
}
}