Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Oculus Grabber on Local Transform

Discussion in 'AR/VR (XR) Discussion' started by firkinfedup, Nov 10, 2018.

  1. firkinfedup

    firkinfedup

    Joined:
    May 6, 2017
    Posts:
    10
    Hi there,

    I'm using the Oculus SDK in unity and implementing the grabber sample. Basically I have a simple prefab that has a long flat plane, and a sphere on top,

    upload_2018-11-10_8-22-58.png

    I'm using the grabber on the sphere to move it up and down the plane. Within the update method of the sphere, I am locking the transform so that it stays on the plane and doesn't fall off.

    Unfortunately even though this is working, it's only working on the global transform. So if I rotate the prefab, the axis that it is locked to, remains the same and the sphere doesn't align with the plane. For example.

    Before rotating prefab...
    upload_2018-11-10_8-25-57.png

    After rotating the prefab...
    upload_2018-11-10_8-26-44.png

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace OVRTouchSample
    4. {
    5.     public class ColorGrabbable : OVRGrabbable
    6.     {
    7.  
    8.         public float MinimumX;
    9.         public float MaximumX;
    10.  
    11.         private Vector3 _startPosGlobal;
    12.         private Quaternion _startRotation;
    13.  
    14.         #region monobehavior methods
    15.  
    16.         void Awake()
    17.         {
    18.             if (m_grabPoints.Length == 0)
    19.             {
    20.                 //Create a grab point from our meshcollider
    21.                 Collider collider = this.GetComponent<Collider>();
    22.                 if (collider == null)
    23.                 {
    24.                     throw new System.ArgumentException("Grabbables cannot have zero grab points and no collider -- please add a grab point or collider.");
    25.                 }
    26.                 m_grabPoints = new Collider[1] { collider };
    27.                 _startPosGlobal = transform.position;
    28.                 _startRotation = transform.rotation;
    29.             }
    30.         }
    31.  
    32.         void Update()
    33.         {
    34.             float newX = transform.position.x;
    35.             if (newX < MinimumX) newX = MinimumX;
    36.             if (newX > MaximumX) newX = MaximumX;
    37.             //we need to move this on its localPosition, not its global position
    38.             transform.position = new Vector3(newX, _startPosGlobal.y, _startPosGlobal.z);
    39.             transform.rotation = _startRotation;
    40.         }
    41.  
    42.         #endregion
    43.  
    44.         #region ovrgrabbable
    45.  
    46.         override public void GrabBegin(OVRGrabber hand, Collider grabPoint)
    47.         {
    48.             base.GrabBegin(hand, grabPoint);
    49.         }
    50.  
    51.         override public void GrabEnd(Vector3 linearVelocity, Vector3 angularVelocity)
    52.         {
    53.             base.GrabEnd(linearVelocity, angularVelocity);
    54.         }
    55.  
    56.         #endregion
    57.  
    58.     }
    59. }
    Simply changing to localPosition from position, doesn't work. Any ideas?
     
  2. firkinfedup

    firkinfedup

    Joined:
    May 6, 2017
    Posts:
    10
    It turns out I was mistaken, just changing it to localPosition does indeed work. Fixed code below.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace OVRTouchSample
    4. {
    5.     public class ColorGrabbable : OVRGrabbable
    6.     {
    7.  
    8.         public float MinimumZ;
    9.         public float MaximumZ;
    10.  
    11.         private Vector3 _startPosGlobal;
    12.         private Vector3 _startPosLocal;
    13.         private Quaternion _startRotation;
    14.  
    15.         #region monobehavior methods
    16.  
    17.         void Awake()
    18.         {
    19.             if (m_grabPoints.Length == 0)
    20.             {
    21.                 //Create a grab point from our meshcollider
    22.                 Collider collider = this.GetComponent<Collider>();
    23.                 if (collider == null)
    24.                 {
    25.                     throw new System.ArgumentException("Grabbables cannot have zero grab points and no collider -- please add a grab point or collider.");
    26.                 }
    27.                 m_grabPoints = new Collider[1] { collider };
    28.                 _startPosGlobal = transform.position;
    29.                 _startPosLocal = transform.localPosition;
    30.                 _startRotation = transform.rotation;
    31.             }
    32.         }
    33.  
    34.         void Update()
    35.         {
    36.             float newZ = transform.localPosition.z;
    37.             if (newZ < MinimumZ) newZ = MinimumZ;
    38.             if (newZ > MaximumZ) newZ = MaximumZ;
    39.             transform.localPosition = new Vector3(_startPosLocal.x, _startPosLocal.y, newZ);
    40.             transform.rotation = _startRotation;
    41.         }
    42.  
    43.         #endregion
    44.  
    45.         #region ovrgrabbable
    46.  
    47.         override public void GrabBegin(OVRGrabber hand, Collider grabPoint)
    48.         {
    49.             base.GrabBegin(hand, grabPoint);
    50.         }
    51.  
    52.         override public void GrabEnd(Vector3 linearVelocity, Vector3 angularVelocity)
    53.         {
    54.             base.GrabEnd(linearVelocity, angularVelocity);
    55.         }
    56.  
    57.         #endregion
    58.  
    59.     }
    60. }