unity vr grabbing code example
Example: Hand Grabbing Script
using UnityEngine;
using System.Collections;
public class GrabObject : MonoBehaviour {
public GameObject hand;
private GameObject grabbedObject = null;
private Transform objectOrgParent = null;
void OnCollisionStay(Collision collision) {
if (Input.GetMouseButton(0) == true) {
grabbedObject = collision.gameObject;
grabbedObject.rigidbody.isKinematic = true;
objectOrgParent = grabbedObject.transform.parent;
grabbedObject.transform.parent = hand.transform;
}
}
void FixedUpdate() {
if (grabbedObject != null) {
if (Input.GetMouseButton(0) == false) {
grabbedObject.transform.parent = objectOrgParent;
grabbedObject.rigidbody.isKinematic = false;
grabbedObject = null;
}
}
}
}