//------------------------------------------------- private void OnHandHoverBegin(Hand hand) { bool showHint = false; // "Catch" the throwable by holding down the interaction button instead of pressing it. // Only do this if the throwable is moving faster than the prescribed threshold speed, // and if it isn't attached to another hand if (!attached) { if (hand.GetStandardInteractionButton()) { Rigidbody rb = GetComponent <Rigidbody>(); if (rb.velocity.magnitude >= catchSpeedThreshold) { hand.AttachObject(gameObject, attachmentFlags, attachmentPoint); showHint = false; } } } if (showHint) { ControllerButtonHints.ShowButtonHint(hand, EVRButtonId.k_EButton_SteamVR_Trigger); } }
//------------------------------------------------- private void HandHoverUpdate(Hand hand) { //Trigger got pressed if (hand.GetStandardInteractionButtonDown()) { hand.AttachObject(gameObject, attachmentFlags, attachmentPoint); ControllerButtonHints.HideButtonHint(hand, EVRButtonId.k_EButton_SteamVR_Trigger); } }
//------------------------------------------------- private void PhysicsAttach(Hand hand) { PhysicsDetach(hand); Rigidbody holdingBody = null; Vector3 holdingPoint = Vector3.zero; // The hand should grab onto the nearest rigid body float closestDistance = float.MaxValue; for (int i = 0; i < rigidBodies.Count; i++) { float distance = Vector3.Distance(rigidBodies[i].worldCenterOfMass, hand.transform.position); if (distance < closestDistance) { holdingBody = rigidBodies[i]; closestDistance = distance; } } // Couldn't grab onto a body if (holdingBody == null) { return; } // Create a fixed joint from the hand to the holding body if (attachMode == AttachMode.FixedJoint) { Rigidbody handRigidbody = Util.FindOrAddComponent <Rigidbody>(hand.gameObject); handRigidbody.isKinematic = true; FixedJoint handJoint = hand.gameObject.AddComponent <FixedJoint>(); handJoint.connectedBody = holdingBody; } // Don't let the hand interact with other things while it's holding us hand.HoverLock(null); // Affix this point Vector3 offset = hand.transform.position - holdingBody.worldCenterOfMass; offset = Mathf.Min(offset.magnitude, 1.0f) * offset.normalized; holdingPoint = holdingBody.transform.InverseTransformPoint(holdingBody.worldCenterOfMass + offset); hand.AttachObject(this.gameObject, attachmentFlags); // Update holding list holdingHands.Add(hand); holdingBodies.Add(holdingBody); holdingPoints.Add(holdingPoint); }
//------------------------------------------------- public void SpawnAndAttach(Hand passedInhand) { Hand handToUse = passedInhand; if (passedInhand == null) { handToUse = hand; } if (handToUse == null) { return; } GameObject prefabObject = Instantiate(prefab) as GameObject; handToUse.AttachObject(prefabObject); }
//------------------------------------------------- void Update() { if (itemPrefab != null) { if (hand.controller != null) { if (hand.controller.hasTracking) { GameObject objectToAttach = GameObject.Instantiate(itemPrefab); objectToAttach.SetActive(true); hand.AttachObject(objectToAttach); hand.controller.TriggerHapticPulse(800); Destroy(gameObject); // If the player's scale has been changed the object to attach will be the wrong size. // To fix this we change the object's scale back to its original, pre-attach scale. objectToAttach.transform.localScale = itemPrefab.transform.localScale; } } } }
//------------------------------------------------- private void SpawnAndAttachObject(Hand hand) { if (hand.otherHand != null) { //If the other hand has this item package, take it back from the other hand ItemPackage otherHandItemPackage = GetAttachedItemPackage(hand.otherHand); if (otherHandItemPackage == itemPackage) { TakeBackItem(hand.otherHand); } } if (showTriggerHint) { ControllerButtonHints.HideTextHint(hand, EVRButtonId.k_EButton_SteamVR_Trigger); } if (itemPackage.otherHandItemPrefab != null) { if (hand.otherHand.hoverLocked) { //Debug.Log( "Not attaching objects because other hand is hoverlocked and we can't deliver both items." ); return; } } // if we're trying to spawn a one-handed item, remove one and two-handed items from this hand and two-handed items from both hands if (itemPackage.packageType == ItemPackage.ItemPackageType.OneHanded) { RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.OneHanded, hand); RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.TwoHanded, hand); RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.TwoHanded, hand.otherHand); } // if we're trying to spawn a two-handed item, remove one and two-handed items from both hands if (itemPackage.packageType == ItemPackage.ItemPackageType.TwoHanded) { RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.OneHanded, hand); RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.OneHanded, hand.otherHand); RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.TwoHanded, hand); RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.TwoHanded, hand.otherHand); } spawnedItem = GameObject.Instantiate(itemPackage.itemPrefab); spawnedItem.SetActive(true); hand.AttachObject(spawnedItem, attachmentFlags, attachmentPoint); if ((itemPackage.otherHandItemPrefab != null) && (hand.otherHand.controller != null)) { GameObject otherHandObjectToAttach = GameObject.Instantiate(itemPackage.otherHandItemPrefab); otherHandObjectToAttach.SetActive(true); hand.otherHand.AttachObject(otherHandObjectToAttach, attachmentFlags); } itemIsSpawned = true; justPickedUpItem = true; if (takeBackItem) { useFadedPreview = true; pickupEvent.Invoke(); CreatePreviewObject(); } }