void Start() { if (!VRManager.IsVREnabled()) { Destroy(this); return; } _headset = VRTK_SDK_Bridge.GetHeadset(); }
public override bool IsConditionOccuring(SDK_BaseGestureLibrary.Hand specificHand, AdvancedGesture advancedGestureInfo) { SDK_BaseGestureLibrary currentLibrary = VRTK.VRTK_SDK_Bridge.GetHandSDK().GetGestureLibrary(); if (currentLibrary == null) { Debug.LogWarning("No gesture library detected for the current Hand SDK"); return(false); } Vector3 palmNormal = currentLibrary.GetHandNormal(specificHand); if (palmNormal == Vector3.zero) { // hand tracking lost return(false); } // otherVec is a normal vector that will be compared to the palm vector Vector3 otherVec; Quaternion rotationToUse = Quaternion.Euler(eulerRotationFromOtherVector); if (otherVectorToUse == OtherVector.World) { switch (otherVectorDirection) { case VectorType.Forward: { otherVec = rotationToUse * Vector3.forward; break; } case VectorType.Up: { otherVec = rotationToUse * Vector3.up; break; } default: { otherVec = rotationToUse * Vector3.up; break; } } } else if (otherVectorToUse == OtherVector.Hmd) { Transform hmd = VRTK_SDK_Bridge.GetHeadset(); switch (otherVectorDirection) { case VectorType.Forward: { otherVec = hmd.TransformDirection(rotationToUse * Vector3.forward); break; } case VectorType.Up: { otherVec = hmd.TransformDirection(rotationToUse * Vector3.up); break; } default: { otherVec = hmd.TransformDirection(rotationToUse * Vector3.up); break; } } } else { // for whatever reason, otherVec is undefined. return false return(false); } float dotProduct = Vector3.Dot(otherVec, palmNormal); // close to 1 -> similar direction if (dotProduct >= 1 - tolerance) { return(true); } else { return(false); } }
//------------------------------------------------- //private void HandAttachedUpdate(Hand hand) private void Update() { if (!isGrab) { return; } // Reset transform since we cheated it right after getting poses on previous frame transform.localPosition = Vector3.zero; transform.localRotation = Quaternion.identity; // Update handedness guess //EvaluateHandedness(); if (nocked) { deferNewPoses = true; Vector3 nockToarrowHand = (arrowHand.arrowNockTransform.parent.position - nockRestTransform.position); // Vector from bow nock transform to arrowhand nock transform - used to align bow when drawing // Align bow // Time lerp value used for ramping into drawn bow orientation float lerp = Util.RemapNumberClamped(Time.time, nockLerpStartTime, (nockLerpStartTime + lerpDuration), 0f, 1f); float pullLerp = Util.RemapNumberClamped(nockToarrowHand.magnitude, minPull, maxPull, 0f, 1f); // Normalized current state of bow draw 0 - 1 //Vector3 arrowNockTransformToHeadset = ((Player.instance.hmdTransform.position + (Vector3.down * 0.05f)) - arrowHand.arrowNockTransform.parent.position).normalized; Vector3 arrowNockTransformToHeadset = ((VRTK_SDK_Bridge.GetHeadset().position + (Vector3.down * 0.05f)) - arrowHand.arrowNockTransform.parent.position).normalized; Vector3 arrowHandPosition = (arrowHand.arrowNockTransform.parent.position + ((arrowNockTransformToHeadset * drawOffset) * pullLerp)); // Use this line to lerp arrowHand nock position //Vector3 arrowHandPosition = arrowHand.arrowNockTransform.position; // Use this line if we don't want to lerp arrowHand nock position Vector3 pivotToString = (arrowHandPosition - pivotTransform.position).normalized; Vector3 pivotToLowerHandle = (handleTransform.position - pivotTransform.position).normalized; bowLeftVector = -Vector3.Cross(pivotToLowerHandle, pivotToString); pivotTransform.rotation = Quaternion.Lerp(nockLerpStartRotation, Quaternion.LookRotation(pivotToString, bowLeftVector), lerp); // Move nock position if (Vector3.Dot(nockToarrowHand, -nockTransform.forward) > 0) { float distanceToarrowHand = nockToarrowHand.magnitude * lerp; nockTransform.localPosition = new Vector3(0f, 0f, Mathf.Clamp(-distanceToarrowHand, -maxPull, 0f)); nockDistanceTravelled = -nockTransform.localPosition.z; arrowVelocity = Util.RemapNumber(nockDistanceTravelled, minPull, maxPull, arrowMinVelocity, arrowMaxVelocity); drawTension = Util.RemapNumberClamped(nockDistanceTravelled, 0, maxPull, 0f, 1f); //this.bowDrawLinearMapping.value = drawTension; // Send drawTension value to LinearMapping script, which drives the bow draw animation longBowAni.Play(0, 0, drawTension); if (nockDistanceTravelled > minPull) { pulled = true; } else { pulled = false; } if ((nockDistanceTravelled > (lastTickDistance + hapticDistanceThreshold)) || nockDistanceTravelled < (lastTickDistance - hapticDistanceThreshold)) { ushort hapticStrength = (ushort)Util.RemapNumber(nockDistanceTravelled, 0, maxPull, bowPullPulseStrengthLow, bowPullPulseStrengthHigh); ///手柄震动 VRTK_SDK_Bridge.HapticPulse(VRTK_ControllerReference.GetControllerReference(hand), hapticStrength); VRTK_SDK_Bridge.HapticPulse(VRTK_ControllerReference.GetControllerReference(otherHand), hapticStrength); //hand.controller.TriggerHapticPulse(hapticStrength); //hand.otherHand.controller.TriggerHapticPulse(hapticStrength); drawSound.PlayBowTensionClicks(drawTension); lastTickDistance = nockDistanceTravelled; } if (nockDistanceTravelled >= maxPull) { if (Time.time > nextStrainTick) { //hand.controller.TriggerHapticPulse(400); //hand.otherHand.controller.TriggerHapticPulse(400); VRTK_SDK_Bridge.HapticPulse(VRTK_ControllerReference.GetControllerReference(hand), 400); VRTK_SDK_Bridge.HapticPulse(VRTK_ControllerReference.GetControllerReference(otherHand), 400); drawSound.PlayBowTensionClicks(drawTension); nextStrainTick = Time.time + Random.Range(minStrainTickTime, maxStrainTickTime); } } } else { nockTransform.localPosition = new Vector3(0f, 0f, 0f); //this.bowDrawLinearMapping.value = 0f; longBowAni.Play(0, 0, 0); } } else { if (lerpBackToZeroRotation) { float lerp = Util.RemapNumber(Time.time, lerpStartTime, lerpStartTime + lerpDuration, 0, 1); pivotTransform.localRotation = Quaternion.Lerp(lerpStartRotation, Quaternion.identity, lerp); if (lerp >= 1) { lerpBackToZeroRotation = false; } } } }