public void UpdateFinger(float deltaTime) { if (deltaTime == 0) { return; } float squeezeValue = 0; if (squeezyAction != null && squeezyAction.GetActive(inputSource)) { squeezeValue = squeezyAction.GetAxis(inputSource); } squeezySmooth = Mathf.Lerp(squeezySmooth, Mathf.Sqrt(squeezeValue), deltaTime * 10); if (renderer.sharedMesh.blendShapeCount > 0) { renderer.SetBlendShapeWeight(0, squeezySmooth * 100); } float boneRot = 0; if (referenceAxis == eulerAxis.X) { boneRot = referenceBone.localEulerAngles.x; } if (referenceAxis == eulerAxis.Y) { boneRot = referenceBone.localEulerAngles.y; } if (referenceAxis == eulerAxis.Z) { boneRot = referenceBone.localEulerAngles.z; } boneRot = FixAngle(boneRot); pos = Mathf.InverseLerp(referenceAngles.x, referenceAngles.y, boneRot); if (mass > 0) { for (int boneIndex = 0; boneIndex < bones.Length; boneIndex++) { bool useOffset = boneTips[boneIndex] != null; if (useOffset) // inertia sim { Vector3 offset = (boneTips[boneIndex].localPosition - bones[boneIndex].InverseTransformPoint(oldTipPosition[boneIndex])) / deltaTime; Vector3 inertia = (offset - oldTipDelta[boneIndex]) / deltaTime; oldTipDelta[boneIndex] = offset; Vector3 drag = offset * -2; inertia *= -2f; for (int offsetIndex = inertiaSteps - 1; offsetIndex > 0; offsetIndex--) // offset inertia steps { inertiaSmoothing[boneIndex, offsetIndex] = inertiaSmoothing[boneIndex, offsetIndex - 1]; } inertiaSmoothing[boneIndex, 0] = inertia; Vector3 smoothedInertia = Vector3.zero; for (int offsetIndex = 0; offsetIndex < inertiaSteps; offsetIndex++) // offset inertia steps { smoothedInertia += inertiaSmoothing[boneIndex, offsetIndex]; } smoothedInertia = smoothedInertia / inertiaSteps; //if (boneIndex == 0 && Input.GetKey(KeyCode.Space)) // Debug.Log(smoothedInertia); smoothedInertia = PowVector(smoothedInertia / 20, 3) * 20; Vector3 forward = forwardAxis; Vector3 forwardDrag = forwardAxis + drag; Vector3 forwardInertia = forwardAxis + smoothedInertia; Quaternion dragQuaternion = Quaternion.FromToRotation(forward, forwardDrag); Quaternion inertiaQuaternion = Quaternion.FromToRotation(forward, forwardInertia); velocity[boneIndex] += FixVector(dragQuaternion.eulerAngles) * 2 * deltaTime; velocity[boneIndex] += FixVector(inertiaQuaternion.eulerAngles) * 50 * deltaTime; velocity[boneIndex] = Vector3.ClampMagnitude(velocity[boneIndex], 1000); } Vector3 targetPos = pos * Vector3.right * (flexAngle / bones.Length); Vector3 springForce = -k * (rotation[boneIndex] - targetPos); var dampingForce = damping * velocity[boneIndex]; var force = springForce - dampingForce; var acceleration = force / mass; velocity[boneIndex] += acceleration * deltaTime; rotation[boneIndex] += velocity[boneIndex] * Time.deltaTime; rotation[boneIndex] = Vector3.ClampMagnitude(rotation[boneIndex], 180); if (useOffset) { oldTipPosition[boneIndex] = boneTips[boneIndex].position; } } } else { Debug.LogError("<b>[SteamVR_Standalone Interaction]</b> finger mass is zero"); } }
void FixedUpdate() { _Player.GetComponent <Rigidbody>().mass = _ViveMass; // The trigger is noisy, looks like only the first digit is reliable. _TriggerVal = 0; if (_throttle_action != null && _throttle_action.GetActive(SteamVR_Input_Sources.Any)) { float value = (float)Math.Round(_throttle_action.GetAxis(SteamVR_Input_Sources.Any), 1); if (value > 0) { _TriggerVal = value; } } _Speed = _Player.GetComponent <Rigidbody>().velocity.magnitude; if (_TriggerVal > 0 && _EnableMovePlatform) { _DirectionArrow.SetActive(true); if (_Speed < _MaxSpeed) { Vector3 directionOfForce = new Vector3(60f, 0f, 0f); Vector3 directionVector = Quaternion.Euler(directionOfForce) * Vector3.forward; if (_move_backwards_action != null && _move_backwards_action.GetActive(SteamVR_Input_Sources.Any)) { if (_move_backwards_action.GetState(SteamVR_Input_Sources.Any)) { directionVector = Quaternion.Euler(directionOfForce) * Vector3.back; _DirectionArrow.transform.localEulerAngles = new Vector3(-58f, 180f, 0f); _DirectionArrow.transform.localPosition = new Vector3(0f, 0f, 0f); } else { _DirectionArrow.transform.localEulerAngles = new Vector3(58f, 0f, 0f); _DirectionArrow.transform.localPosition = new Vector3(0f, -0.0609f, 0.038f); } } Vector3 forceDirection = _RightHand.transform.rotation * directionVector * _ViveMoveForce * (float)_TriggerVal; _Player.GetComponent <Rigidbody>().drag = _ViveNormalDrag; _Player.GetComponent <Rigidbody>().AddForce(forceDirection, ForceMode.Acceleration); _Player.GetComponent <Rigidbody>().maxAngularVelocity = 2f; } else { float brakeSpeed = _Speed - _MaxSpeed; Vector3 normalisedVelocity = _Player.GetComponent <Rigidbody>().velocity.normalized; Vector3 brakeVelocity = normalisedVelocity * brakeSpeed * _ViveBreakFactor; _Player.GetComponent <Rigidbody>().AddForce(-brakeVelocity); } } else { _DirectionArrow.SetActive(false); _Player.GetComponent <Rigidbody>().drag = _ViveStopDrag; } }