private bool isFistAndMovingInDirection(TrackedHand hand, float lookBackWindowInSeconds, Vector3 direction, float distanceThreshold) { int lookBackWindowIndex = hand.getIndexForPastTimeSeconds(lookBackWindowInSeconds); // Validate that we have enough position memory if (hand.pastControllerPositions.Count < lookBackWindowIndex) { // Debug.Log("Not enough look back positions"); return(false); } for (int i = 0; i < lookBackWindowIndex; i++) { // Validate validity requirement if (!hand.pastControllerPositions[i].isValid) { // Debug.Log("Not enough valid look back positions"); return(false); } // Validate grip requirement if (!hand.pastControllerPositions[i].isGripping) { // Debug.Log("Hands were not gripped long enough"); return(false); } // Validate distance travelled requirement Vector3 distanceTravelled = hand.getPosition() - hand.pastControllerPositions[i].position; float distanceTravelledInDirection = Vector3.Dot(distanceTravelled, direction.normalized); if (distanceTravelledInDirection > distanceThreshold) { return(true); } } return(false); }
private void analyzeDynamicGestures() { if (!abilityActivated) { // Check wall making gesture if (bothFistsMovingUp() && !isJumping) { Debug.Log("Triggering wall summon!"); abilityActivated = true; wallIsMaking = true; summonWall(); } // Check Jumping ability if (bothFistsMovingDown() && !isJumping) { Debug.Log("Triggering jump!"); abilityActivated = true; jump(leftHand, rightHand); } // Check rock lifting gesture else if (isFistAndMovingUp(leftHand) && bothHandsGestureTimer <= 0.0) { Debug.Log("Triggering left rock summon!"); abilityActivated = true; rockIsActive = true; summonRock(leftHand); } else if (isFistAndMovingUp(rightHand) && bothHandsGestureTimer <= 0.0) { Debug.Log("Triggering right rock summon!"); abilityActivated = true; rockIsActive = true; rightHandAbilityIsActive = true; summonRock(rightHand); } } else if (rockObject != null && abilityActivated && rockIsActive) { TrackedHand punchingHand = rightHandAbilityIsActive ? leftHand : rightHand; if (isFistAndMovingInDirection(punchingHand, lookBackWindowInSeconds, rockObject.transform.position - punchingHand.getPosition(), punchDistanceThreshold)) { if ((rockObject.transform.position - punchingHand.getPosition()).magnitude < rockPunchActivationDistance) { Debug.Log("Triggering rock punch!"); rockIsActive = false; punchRock(rockObject.transform.position - punchingHand.pastControllerPositions[punchingHand.getIndexForPastTimeSeconds(lookBackWindowInSeconds) - 1].position, punchingHand.getVelocity().sqrMagnitude * 250f); } } } }
// ------------------------------------------------------- Jumping Methods ------------------------------------------------------- // Using the average vector over the last 'lookBackWindowInSeconds' between two hands, determines the direction in which to make the player jump. private void jump(TrackedHand left, TrackedHand right) { Debug.Log("Jumped"); Vector3 leftMovementVector = left.getPosition() - left.pastControllerPositions[left.getIndexForPastTimeSeconds(lookBackWindowInSeconds) - 1].position; Vector3 rightMovementVector = right.getPosition() - right.pastControllerPositions[left.getIndexForPastTimeSeconds(lookBackWindowInSeconds) - 1].position; Vector3 midway = leftMovementVector.normalized + rightMovementVector.normalized; float averageVelocity = (left.getVelocity().magnitude + right.getVelocity().magnitude) / 2.0f; spawnJumpRock(midway, averageVelocity); playerRigidBody.AddForce(-1f * midway.normalized * averageVelocity * jumpSpeed);; }