Skip to content

Instantly share code, notes, and snippets.

@botamochi6277
Last active January 24, 2020 01:33
Show Gist options
  • Save botamochi6277/ed93c213acb9151910047f845385aa2c to your computer and use it in GitHub Desktop.
Save botamochi6277/ed93c213acb9151910047f845385aa2c to your computer and use it in GitHub Desktop.
CustomOVR Grabber and Grabbable Set
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class CustomOVRGrabbable : OVRGrabbable, IfGrabbable {
[SerializeField]
protected Transform m_bodyTrans;
[SerializeField]
protected Transform m_grabTrans;
// set initial values.
protected Vector3 m_grabInitPos = Vector3.zero;
protected Quaternion m_grabInitRot = Quaternion.identity;
protected Vector3 m_bodyInitPos = Vector3.zero;
protected Quaternion m_bodyInitRot = Quaternion.identity;
protected OVRInput.Controller m_currentController;
protected bool m_prevIsGrabbed = false;
protected Vector3 m_ungrabMove = Vector3.zero;
protected Quaternion m_ungrabRot = Quaternion.identity;
protected UnityAction m_grabBeginCallback;
protected UnityAction m_grabEndCallback;
// [SerializeField]
// protected float m_grabFreq = 10f;
// [SerializeField]
// protected float m_grabVolume = 1.0f;
private Transform m_trans;
private int m_startCnt = 0;
public OVRInput.Controller controller{
get{
return m_currentController;
}
}
public Transform bodyTf{
get{
return m_bodyTrans;
}
}
public void SetController(OVRInput.Controller controller) {
m_currentController = controller;
}
public void MoveInHand(Vector3 pos, Quaternion rot) {
if (m_startCnt == 0) {
this.Start();
}
this.SetGrabbedArrangement();
m_trans.position = pos;
m_trans.rotation = rot;
this.SetUngrabbedArrangement();
}
// When this object is grabbed, m_grabTrans is the origin of this object.
public override void GrabBegin(OVRGrabber hand, Collider grabPoint) {
base.GrabBegin(hand, grabPoint);
this.SetGrabbedArrangement();
// Destoroy Joints
// var joints = GetComponents<Joint>();
// foreach (var j in joints) {
// Destroy(j);
// }
//
// OVRInput.SetControllerVibration(m_grabFreq,m_grabVolume,m_currentController);
if(m_grabBeginCallback!=null){
m_grabBeginCallback();
}
}
public override void GrabEnd(Vector3 linearVelocity, Vector3 angularVelocity) {
base.GrabEnd(linearVelocity, angularVelocity);
this.SetUngrabbedArrangement();
if(m_grabEndCallback!=null){
m_grabEndCallback();
}
}
public void SetGrabBeginCallback(UnityAction callback){
m_grabBeginCallback = callback;
}
public void SetGrabEndCallback(UnityAction callback){
m_grabEndCallback = callback;
}
private void SetGrabbedArrangement() {
// move grabTrans to origin with bodyTrans
if (m_snapPosition &&
(m_bodyTrans != null) &&
(m_grabTrans != null)) {
m_grabTrans.localPosition = Vector3.zero;
m_bodyTrans.localPosition = m_bodyInitPos - m_grabInitPos;
}
// rotate grabTrans to default rotation with bodyTrans
if (m_snapOrientation &&
(m_bodyTrans != null) &&
(m_grabTrans != null)) {
m_grabTrans.localRotation = Quaternion.identity;
m_bodyTrans.localRotation = Quaternion.Inverse(m_grabInitRot) * m_bodyInitRot;
m_bodyTrans.localPosition = Quaternion.Inverse(m_grabInitRot) * m_bodyTrans.localPosition;
}
}
private void SetUngrabbedArrangement() {
if ((m_bodyTrans != null) &&
(m_grabTrans != null)) {
m_ungrabMove = m_bodyTrans.position ;
m_ungrabRot = m_bodyTrans.rotation ;
m_grabTrans.localPosition = m_grabInitPos;
m_bodyTrans.localPosition = m_bodyInitPos;
m_grabTrans.localRotation = m_grabInitRot;
m_bodyTrans.localRotation = m_bodyInitRot;
m_ungrabMove -= m_bodyTrans.position ;
Debug.LogFormat("ungrab-move: {0}", m_ungrabMove);
m_ungrabRot *= Quaternion.Inverse(m_bodyTrans.rotation);
m_trans.position += m_ungrabMove;
float angle = 0.0F;
Vector3 axis = Vector3.zero;
m_ungrabRot.ToAngleAxis(out angle, out axis);
m_trans.RotateAround(m_bodyTrans.position, axis, angle);
}
}
override protected void Start() {
base.Start();
if (m_grabTrans != null) {
m_grabInitPos = m_grabTrans.localPosition;
m_grabInitRot = m_grabTrans.localRotation;
}
if (m_bodyTrans != null) {
m_bodyInitPos = m_bodyTrans.localPosition;
m_bodyInitRot = m_bodyTrans.localRotation;
}
m_trans = transform;
m_startCnt += 1;
}
}
using UnityEngine;
public class CustomOVRGrabber : OVRGrabber
{
protected override void GrabBegin()
{
base.GrabBegin(); // OVRGrabber.GrabBegin()
if (m_grabbedObj is IfGrabbable)
{
((IfGrabbable)m_grabbedObj).SetController(m_controller);
}
}
public OVRInput.Controller controller{
get{
return this.m_controller;
}
}
}
public interface IfGrabbable
{
void SetController(OVRInput.Controller controller);
}
@botamochi6277
Copy link
Author

Add callbacks, Remove start of CustomOVRGrabbable

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