void Start() { player = transform.root.gameObject; GrabCollider = GameObject.Find("GrabCollider").GetComponent <GrabCollider> (); playerRigid = player.GetComponent <Rigidbody> (); audioSource = GetComponent <AudioSource> (); }
// Use this for initialization public void GenerateGrabObject() { #if UNITY_EDITOR // Create a child GameObject gobj = new GameObject("GrabCollider"); gobj.transform.parent = transform; gobj.transform.localPosition = Vector3.zero; gobj.transform.localRotation = Quaternion.identity; gobj.transform.localScale = Vector3.one; // Copy collider, add rigidbody Collider cd = GetComponent <Collider>(); UnityEditorInternal.ComponentUtility.CopyComponent(cd); UnityEditorInternal.ComponentUtility.PasteComponentAsNew(gobj); Collider ncd = gobj.GetComponent <Collider>(); ncd.isTrigger = true; Rigidbody nrb = gobj.AddComponent <Rigidbody>(); nrb.useGravity = false; // Add Script GrabCollider grabc = gobj.AddComponent <GrabCollider>(); grabc.SetBindObject(transform); grabc.newMaterial = (Material)AssetDatabase.LoadAssetAtPath("Assets/Materials/OutlineEffect/OutlineEffect/VerticesOutline.mat", typeof(Material)); grabc.AutomaticExpand = true; #endif }
/* * //Method aborted * private bool isGrabGesture(){ * GameObject thumb_2 = this.transform.GetChild (0).GetChild (2).gameObject; * GameObject indexfinger_2 = this.transform.GetChild (1).GetChild (2).gameObject; * float dist_thumb_index = Vector3.Distance(thumb_2.transform.position, indexfinger_2.transform.position); * if (dist_thumb_index < 0.065){ * return true; * } * return false; * } */ /* grabObject * Input: GameObject obj * Output: None * Summary: 1. Reset and inactivate rigidbody of current obj. Otherwise obj could "magically" move in your hand :) 2. Set obj to move with hand */ private void grabObject(GameObject obj) { obj.GetComponent <Collider> ().isTrigger = true; /*new feature: push*/ if (push_enabled) { if (coroutine != null) { StopCoroutine(coroutine); } palm.GetComponent <Collider> ().isTrigger = true; } obj.GetComponent <Rigidbody> ().useGravity = false; obj.GetComponent <Rigidbody> ().velocity = Vector3.zero; obj.GetComponent <Rigidbody> ().angularVelocity = Vector3.zero; obj.GetComponent <Rigidbody> ().Sleep(); // When grabbing, highlight it. GrabCollider grabCol = obj.GetComponentInChildren <GrabCollider>(); if (grabCol != null) { grabCol.OnBeingGrabbed(); } inverseGrabRotation = Quaternion.Inverse(grabHolder.transform.rotation); grabDeltaPosition = obj.transform.position - grabHolder.transform.position; //this was used to make obj a child //obj.transform.SetParent(grabHolder.transform); //trying this right now: //initialize MA array objBuffer = new Vector3[smoothingBuffer]; for (int i = 0; i < smoothingBuffer; i++) { objBuffer [i] = obj.transform.position; } this.updateObjTransform(obj); /* pulasation */ /* before invoke, check 2 things. if it is enabled, if there are existing ones */ /* FIX!!!*/ //InvokeRepeating ("VPulse", 1.0f, 1.0f); is_grabbing = true; }
public static void SetGrabableLegacy() { GameObject targetObj = Selection.activeGameObject; if (targetObj != null) { Collider cd = targetObj.GetComponent <Collider>(); if (cd == null) { UnityEditor.EditorUtility.DisplayDialog("Error", "The grabable object must have a collider" + "component", "OK"); return; } // check ridigbody Rigidbody rd = targetObj.GetComponent <Rigidbody>(); if (rd == null) { rd = targetObj.AddComponent <Rigidbody>(); rd.useGravity = false; } // Create child GameObject checkobj = ObjectFactory.CreateGameObject("GrabCollider"); targetObj.layer = 11; checkobj.transform.parent = targetObj.transform; checkobj.transform.localPosition = Vector3.zero; checkobj.transform.localRotation = Quaternion.identity; checkobj.transform.localScale = Vector3.one; // Copy collider UnityEditorInternal.ComponentUtility.CopyComponent(cd); UnityEditorInternal.ComponentUtility.PasteComponentAsNew(checkobj); // Set trigger Collider ccd = checkobj.GetComponent <Collider>(); ccd.isTrigger = true; // Add rigidbody Rigidbody crb = checkobj.AddComponent <Rigidbody>(); crb.useGravity = false; // Add Script GrabCollider cgc = checkobj.AddComponent <GrabCollider>(); cgc.AutomaticExpand = true; cgc.newMaterial = AssetDatabase.LoadAssetAtPath(outlineMaterialPath, typeof(Material)) as Material; cgc.SetBindObject(targetObj.transform); // Set tag targetObj.tag = "InteractableObj"; } }
// Update is called once per frame void Update() { // Debug.Log(bIsLeftHand + " Gesture:" + gestureManager.bufferedGesture()); if (HandActionRecog.getInstance() != null && HandActionRecog.getInstance().IsMotion("OpenMenu", bIsLeftHand)) { contextSwitch("menu"); } switch (context) { case "menu": break; case "paint": break; default: GameObject interact_obj = getHandObject(); if (interact_obj != null) { cleanGuidance(); //if hand gesture is grabbing string cur_gesture = gestureManager.bufferedGesture(); if (cur_gesture == "pinch" || cur_gesture == "fist") { //grab objbect if hand is not grabbing if (!is_grabbing) { grabObject(interact_obj); } else { this.updateObjTransform(interact_obj); } //Debug.Log ("obj isTrigger is " + interact_obj.GetComponent<Collider>().isTrigger + "palm isTrigger is " + palm.GetComponent<Collider>().isTrigger + "palm angular v is " + palm.GetComponent<Rigidbody>().angularVelocity); //if hand gesture is not grabbing } else { //but hand is grabbing Transform indextip = transform.Find("index").Find("bone3"); Transform thumbtip = transform.Find("thumb").Find("bone3"); float index_thumb_dis = Vector3.Distance(indextip.position, thumbtip.position); if (is_grabbing && (cur_gesture == "palm" || index_thumb_dis > 0.04f)) { Debug.Log("Grab:Release Grab, current gesture:" + gestureManager.bufferedGesture()); //then tell the object to release itself, Here support two version of interaction objects. InteractionScriptObject iso = interact_obj.GetComponent <InteractionScriptObject> (); if (iso != null && iso.isActiveAndEnabled) { iso.releaseSelf(); } else { GrabCollider gc = interact_obj.GetComponentInChildren <GrabCollider> (); if (gc != null) { gc.OnGrabFinished(); } } //releaseObject (interact_obj); CancelInvoke(); } else if (is_grabbing) { this.updateObjTransform(interact_obj); } } /* nothing in hand (interactable object is null)*/ } else { if (gestureManager.bufferedGesture() == "palm") { hitObject(); } else { cleanGuidance(); } } break; } updateHandSpeed(); }