Skip to content

Instantly share code, notes, and snippets.

@tshaddix
Last active November 19, 2020 01:32
Show Gist options
  • Save tshaddix/e9edb7769c4791e72bdb7285d6558bcd to your computer and use it in GitHub Desktop.
Save tshaddix/e9edb7769c4791e72bdb7285d6558bcd to your computer and use it in GitHub Desktop.
Oculus uses a property called `snapOffset` in its sample framework to help snap a holdable object to a position in a hand whenever it is grabbed. This is handy, but the way it is implemented is pretty janky and uses a local rotation of the hand before applying the transformation. If you use something like an Oculus Quest, it takes way too long t…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SnapPositionHelper : MonoBehaviour
{
public OVRGrabber hand;
public Transform snapOffset;
void Update()
{
var m_posOff = hand.gripTransform.localPosition;
Vector3 snapOffsetV = snapOffset.position;
if (hand.controller == OVRInput.Controller.LTouch) snapOffsetV.x = -snapOffsetV.x;
m_posOff += snapOffsetV;
var m_rotOff = hand.gripTransform.localRotation;
var snapRotation = snapOffset.rotation;
if (hand.controller == OVRInput.Controller.LTouch) snapRotation = Quaternion.Inverse(snapRotation);
m_rotOff = snapRotation * m_rotOff;
var pos = new Vector3(0, 0, 0);
var rot = Quaternion.Euler(0, 0, hand.controller == OVRInput.Controller.LTouch ? 90 : -90);
Vector3 grabbablePosition = pos + rot * m_posOff;
Quaternion grabbableRotation = rot * m_rotOff;
transform.position = grabbablePosition;
transform.rotation = grabbableRotation;
}
}
@ryanlogan
Copy link

I'd love to try this out but I can't see all the comments at the top. How is this intended to be used exactly? I have figured out how to get the right hand mapped using their "snappffset", but would love to do the left hand without trial and error. I would think "hand" would be set based on the hand trying to grab the object. Is this script meant to attach to the object being grabbed? Thanks for any details that you might have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment