Skip to content

Instantly share code, notes, and snippets.

@kazumalab
Created February 16, 2017 12:00
Show Gist options
  • Save kazumalab/0d10afc484a3c4caeb837728e243b17b to your computer and use it in GitHub Desktop.
Save kazumalab/0d10afc484a3c4caeb837728e243b17b to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class OVRHand : MonoBehaviour {
private GameObject holdPosition;
private OVRInput.RawButton whichHand;
public Transform haveItem;
public OVRHand otherHand;
public OVRInput.Controller whichController;
private void Awake () {
if (whichController == OVRInput.Controller.RTouch) {
whichHand = OVRInput.RawButton.RHandTrigger;
}
if (whichController == OVRInput.Controller.LTouch) {
whichHand = OVRInput.RawButton.LHandTrigger;
}
}
// Use this for initialization
void Start () {
holdPosition = transform.GetChild (0).gameObject;
}
// Update is called once per frame
void Update () {
if (haveItem != null) {
if (OVRInput.GetUp (whichHand)) {
Release ();
}
}
}
void OnTriggerStay (Collider col) {
if (haveItem == null) {
if (col.gameObject.tag == "holdable") {
if (OVRInput.GetDown (whichHand)) {
if (col.transform == otherHand.haveItem) {
otherHand.haveItem = null;
}
Hold (col.gameObject);
}
}
}
}
public void Hold (GameObject obj) {
haveItem = obj.transform;
Rigidbody rb = obj.GetComponent<Rigidbody> ();
rb.isKinematic = true;
rb.useGravity = false;
obj.transform.position = holdPosition.transform.position;
obj.transform.parent = holdPosition.transform;
}
public void Release () {
haveItem.parent = null;
Rigidbody rb = haveItem.GetComponent<Rigidbody> ();
rb.isKinematic = false;
rb.useGravity = true;
rb.velocity = OVRInput.GetLocalControllerVelocity (whichController);
haveItem = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment