56 lines
2.5 KiB
C#
56 lines
2.5 KiB
C#
using Unity.Mathematics.Geometry;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UIElements;
|
|
using UnityEngine.XR.Interaction.Toolkit;
|
|
using UnityEngine.XR.Interaction.Toolkit.Interactables;
|
|
using UnityEngine.XR.Interaction.Toolkit.Transformers;
|
|
|
|
public class XRBoundGrabTransformer : XRSingleGrabFreeTransformer
|
|
{
|
|
[Header("Position Bounds")]
|
|
public bool XAxisBound;
|
|
public float XAxisMin;
|
|
public float XAxisMax;
|
|
public bool YAxisBound;
|
|
public float YAxisMin;
|
|
public float YAxisMax;
|
|
public bool ZAxisBound;
|
|
public float ZAxisMin;
|
|
public float ZAxisMax;
|
|
|
|
private Vector3 _centralPos;
|
|
private Vector3 XPosMin => transform.parent ? transform.parent.TransformPoint(_centralPos + new Vector3(XAxisMin, 0, 0)) : _centralPos + new Vector3(XAxisMin, 0, 0);
|
|
private Vector3 XPosMax => transform.parent ? transform.parent.TransformPoint(_centralPos + new Vector3(XAxisMax, 0, 0)) : _centralPos + new Vector3(XAxisMax, 0, 0);
|
|
private Vector3 YPosMin => transform.parent ? transform.parent.TransformPoint(_centralPos + new Vector3(0, YAxisMin, 0)) : _centralPos + new Vector3(0, YAxisMin, 0);
|
|
private Vector3 YPosMax => transform.parent ? transform.parent.TransformPoint(_centralPos + new Vector3(0, YAxisMax, 0)) : _centralPos + new Vector3(0, YAxisMax, 0);
|
|
private Vector3 ZPosMin => transform.parent ? transform.parent.TransformPoint(_centralPos + new Vector3(0, 0, ZAxisMin)) : _centralPos + new Vector3(0, 0, ZAxisMin);
|
|
private Vector3 ZPosMax => transform.parent ? transform.parent.TransformPoint(_centralPos + new Vector3(0, 0, ZAxisMax)) : _centralPos + new Vector3(0, 0, ZAxisMax);
|
|
private bool _isTouchingBound;
|
|
|
|
private void Awake()
|
|
{
|
|
ResetCenter();
|
|
}
|
|
|
|
public void ResetCenter()
|
|
{
|
|
_centralPos = transform.position;
|
|
}
|
|
|
|
public override void Process(XRGrabInteractable grabInteractable, XRInteractionUpdateOrder.UpdatePhase updatePhase, ref Pose targetPose, ref Vector3 localScale)
|
|
{
|
|
base.Process(grabInteractable, updatePhase, ref targetPose, ref localScale);
|
|
ClampPosition(ref targetPose);
|
|
}
|
|
|
|
private void ClampPosition(ref Pose targetPose)
|
|
{
|
|
targetPose.position = new Vector3(
|
|
XAxisBound ? Mathf.Clamp(targetPose.position.x, XPosMin.x, XPosMax.x) : targetPose.position.x,
|
|
YAxisBound ? Mathf.Clamp(targetPose.position.y, YPosMin.y, YPosMax.y) : targetPose.position.y,
|
|
ZAxisBound ? Mathf.Clamp(targetPose.position.z, ZPosMin.z, ZPosMax.z) : targetPose.position.z
|
|
);
|
|
}
|
|
}
|