Search Unity

VR - Oculus SDK - Touch controller - Snap Position , snap offset - grab a gun

Discussion in 'AR/VR (XR) Discussion' started by moiav, Jan 16, 2018.

  1. moiav

    moiav

    Joined:
    Jan 16, 2018
    Posts:
    1
    Hello everybody,
    i'm developping a VR project in 2018 with the standard Oculus SDK (Utilities, Avatar,Platform).
    i have a very simple scene with a table, some cube on , a mug and a gun.
    When i play the game everything is ok in VR with the oculus, i see my hands and i can grab/throw objects.

    But i need you cause when i grad the gun, its not in correct position in my VR hand.
    i see the parameter in OVR grabbable "Snap position, Snap offset" but i dont understant how it work.

    Can someone of you can explain me how to specify the orientation of the gun when i grad it ?

    I do lot of search on the web but find nothing.

    Thank you :)
     
  2. dyljos

    dyljos

    Joined:
    Dec 13, 2017
    Posts:
    1
    set up an empty gameobject at position (0,0,0) and rotation (0,0,0) in the unity scene. attach this as your snap offset transform in the ovr grabbable script. change the rotation or position values of this empty object by the amount you want the object to offset when you grab it.
     
    threebeerhonesty likes this.
  3. Azorah

    Azorah

    Joined:
    Mar 5, 2017
    Posts:
    2
    Do you have a video on this maybe? I'm not understanding it


     
  4. edufireheart

    edufireheart

    Joined:
    Aug 6, 2017
    Posts:
    11
    I was struggling too to understand how it works. Now with the explanation above I think I've got it.
    Honestly I think it's not the best approach to have to pass a Transform to the grabber by the grabbable's script since a Transform needs a GameObject to exist. But unfortunately it's how it's implemented here, I can change that, I know; but it doesn't seem as it was one of the best ideas of how implementing that.
    Our problem is even worse because the grabbable will use the global position and rotation from the transform passed as argument, when it would actually be better being able to attach a small GameObject to the grabbable's gameObject and set its local position and rotation accordingly, exactly as a "offset" should work.

    So here's the deal: The grabber needs a Vector3 and a rotation. They've implemented the need of it by passing a Transform to the Grabber (which contains both) as an offset of the grabbable's gameObject. Since the grabber will use the global position and rotation of this Transform and the fact that a Transform doesn't exist without a GameObject, you need a brand new GameObject with its Transform set to the offset you want
     
  5. edufireheart

    edufireheart

    Joined:
    Aug 6, 2017
    Posts:
    11
    Actually, forget what I said. This grabber transform thing is a really terrible implementation of the matter. It's not at all done as players and developers would expect to use it. I will need to modify it to a more intuitive usability. I'll try to post it here as soon as I get the time to make this change.
     
  6. edufireheart

    edufireheart

    Joined:
    Aug 6, 2017
    Posts:
    11
    Hey, I've changed the original grabber script and I have something that is more intuitive for me (and probably everyone else too) than the original Oculus script. I see that some problems come from the fact this SDK is sometimes presented only as a demo and sometimes as a final product ready to use by developers, what gives us some headaches.
    To the point:
    1) Create an empty GameObject as a child of your grabbable object and adjust its position and rotation to place it where you want your hand to be.
    2) Place this new GameObject in the Snap Offset slot of your grabbable GameObject.
    3) Change these two pieces of code from OVRGrabber:
    Code (CSharp):
    1. m_grabbedObjectPosOff = m_gripTransform.localPosition;
    2. if(m_grabbedObj.snapOffset)
    3. {
    4.     Vector3 snapOffset = m_grabbedObj.snapOffset.position;
    5.     if (m_controller == OVRInput.Controller.LTouch) snapOffset.x = -snapOffset.x;
    6.     m_grabbedObjectPosOff += snapOffset;
    7. }
    Code (CSharp):
    1. m_grabbedObjectRotOff = m_gripTransform.localRotation;
    2. if(m_grabbedObj.snapOffset)
    3. {
    4.     m_grabbedObjectRotOff = m_grabbedObj.snapOffset.rotation * m_grabbedObjectRotOff;
    5. }
    by respectively the following ones:

    Code (CSharp):
    1. if (m_grabbedObj.snapOffset)
    2. {
    3.     Vector3 snapOffset = -m_grabbedObj.snapOffset.localPosition;
    4.     Vector3 snapOffsetScale = m_grabbedObj.snapOffset.lossyScale;
    5.     snapOffset = new Vector3(snapOffset.x * snapOffsetScale.x, snapOffset.y * snapOffsetScale.y, snapOffset.z * snapOffsetScale.z);
    6.     if (m_controller == OVRInput.Controller.LTouch)
    7.     {
    8.         snapOffset.x = -snapOffset.x;
    9.     }
    10.     m_grabbedObjectPosOff = snapOffset;
    11. }
    12. else
    13. {
    14.     m_grabbedObjectPosOff = Vector3.zero;
    15. }
    Code (CSharp):
    1. if (m_grabbedObj.snapOffset)
    2. {
    3.     m_grabbedObjectRotOff = Quaternion.Inverse(m_grabbedObj.snapOffset.localRotation);
    4. }
    5. else
    6. {
    7.     m_grabbedObjectRotOff = Quaternion.identity;
    8. }
    There you go
     
    elpko, shacharoz, arrezes and 12 others like this.
  7. jeromeWork

    jeromeWork

    Joined:
    Sep 1, 2015
    Posts:
    429
  8. dimitur_kolev2

    dimitur_kolev2

    Joined:
    Nov 2, 2015
    Posts:
    19
    I simply added that and it worked :)


    virtual public void GrabBegin(OVRGrabber hand, Collider grabPoint)
    {
    m_grabbedBy = hand;
    m_grabbedCollider = grabPoint;
    gameObject.GetComponent<Rigidbody>().isKinematic = true;
    gameObject.transform.position = hand.transform.position;
    }
     
  9. peacha

    peacha

    Joined:
    Feb 8, 2018
    Posts:
    1
    It's still very complicated to use because there's no way of knowing what the transform will look like in game. Right now I'm just changing positions/rotation values randomly and hoping that when I see the result in-game it will line up. Typically it takes me an hour per grabbable object to guess the correct transform. It should really be a three minute job where you can visualize the grabbable transform rather than guessing in the dark.
     
    graworg likes this.
  10. edufireheart

    edufireheart

    Joined:
    Aug 6, 2017
    Posts:
    11
    I will just add this as a simple commentary. Regarding VR in Unity, since I discovered VRTK, I don't really bother trying anything else anymore. The problem is just installing the correct VRTK version with the corresponding working SteamVR version, since they're not necessarily the ones from Unity AssetStore. I got the correct ones looking at its Slack page. With that I could find many simplifications of what we usually try to implement for VR games, like snap on grab, teleportation, using grabbed items, etc
     
    anattress likes this.
  11. DemonGamesLab

    DemonGamesLab

    Joined:
    Nov 10, 2015
    Posts:
    9
    @edufireheart you are amazing! this has helped a lot! Thank you for the code share
     
  12. rahul20dec

    rahul20dec

    Joined:
    Jul 8, 2018
    Posts:
    9
    Thank you so much!!
     
  13. PrairieDogSeeksHeart

    PrairieDogSeeksHeart

    Joined:
    Oct 27, 2017
    Posts:
    5
    This works very well. You saved me a lot of frustration... Thanks for sharing!
     
  14. Imnpsnm

    Imnpsnm

    Joined:
    Jul 22, 2012
    Posts:
    3
    Working awesomely
     
  15. breenen

    breenen

    Joined:
    Aug 7, 2018
    Posts:
    5
    Thanks @edufireheart that worked for me
    So confused why this hasnt been fixed in the current Oculus Integration 12.0 version still, it's a critical bug that stops us from being able to use the OVRGrabbable scripts.
    (My worry, is when/if I update my version of Oculus Integration, this issue will come back again... and if that is a year down the line, i may forget about this fix)
     
  16. breenen

    breenen

    Joined:
    Aug 7, 2018
    Posts:
    5
    ok, this is how I fixed it now (without needing to change the script)
    Remove local avatar from TrackingSpace
    Drag the "CustomHandRight" and "CustomHandLeft" into the "LeftHandAnchor" and "RightHandAnchor"
    In the components for the "CustomHandRight" and "CustomHandLeft", tick the "Parent Held Object" (otherwise for me, the object will snap under the player and get stuck there.... strange)

    Use this as a guide:

    from 0.25 he remotes the LocalAvatar, but read the comments from Valam and ShadowDog (Im just trying to figure out why my hands wont show, but other than that, it is working
     
    masta-yoda likes this.
  17. AccentDave

    AccentDave

    Joined:
    Nov 16, 2015
    Posts:
    43
    edufireheart, thanks for the code snips. I found that for some reason the snap point was mirrored in X across the object's pivot, but easy to compensate, saved me a ton of headaches. I also tried to override the OVRGrabber class, in hopes of not having to hack into OVR code, but 'OVRGrabber is undefined' for some reason. Hoping this is fixed officially, or XR Inputs or some toolkit will have it right.
     
  18. pokox

    pokox

    Joined:
    Feb 18, 2020
    Posts:
    2
    This works pretty well! the only thing is that is snaps my weapon to hight. when u change the y of the grabber child object it doesnt change it when i build it or it just snaps it random somewhere else. U maybe have a idea how i can fix this?