示例#1
0
        public float GetCustomPinchDistance(Hand h)
        {
            Vector3 c0, c1;
            var     pinchDistance = PinchSegment2SegmentDisplacement(h, out c0, out c1).magnitude;

            pinchDistance -= 0.01f;
            pinchDistance  = pinchDistance.Clamped01();

//      if (Input.GetKeyDown(KeyCode.C)) {
//        Debug.Log(pinchDistance);
//      }

            if (drawDebugPinchDistance)
            {
                DebugPing.Line("RH pinch", c0, c1, LeapColor.blue);
                DebugPing.Label("RH pinch",
                                labelText: pinchDistance.ToString("F3"),
                                labeledPosition: ((c1 + c0) / 2f),
                                color: LeapColor.blue);
            }

            return(pinchDistance);
        }
        private void Update()
        {
            // Current facing.
            var curFacing = this.transform.forward.xz();

            // Movement input.
            float moveVecMag;
            var   moveVec          = getInputMovementVector(out moveVecMag);
            var   moveVecDir       = (moveVecMag > 0.01f ? moveVec / moveVecMag : curFacing);
            var   isMovingIntended = moveVecMag > 0.01f;

            if (cameraTransform != null && isMovingIntended)
            {
                var camXZToPlayer
                    = this.transform.position.ProjectedOnPlane(this.transform.up)
                      - cameraTransform.position.ProjectedOnPlane(this.transform.up);
                var camXZAngle  = Vector3.SignedAngle(camXZToPlayer, Vector3.forward, Vector3.up);
                var camRotation = Quaternion.AngleAxis(-camXZAngle, Vector3.up);
                moveVec    = (camRotation * moveVec.AsXZ()).xz();
                moveVecDir = (camRotation * moveVecDir.AsXZ()).xz();
            }
            if (drawDebug)
            {
                DebugPing.Line("moveVec", this.transform.position,
                               this.transform.position + moveVec.AsXZ(), LeapColor.red);
            }

            // Movement.
            if (!_lastPosMem.HasValue)
            {
                _lastPosMem = this.transform.position;
            }
            var curVelXZ = ((this.transform.position - _lastPosMem.Value) / Time.deltaTime).xz();

            if (isMovingIntended)
            {
                curVelXZ  = curVelXZ.RotatedTowards(moveVecDir, turnSpeed * Time.deltaTime);
                curVelXZ += speed * moveVec * Time.deltaTime;
            }
            curVelXZ                *= 1 - (movementDecay * Time.deltaTime).Clamped01();
            _lastPosMem              = this.transform.position;
            this.transform.position += curVelXZ.AsXZ() * Time.deltaTime;
            var curSpdXZ = curVelXZ.magnitude;

            // Facing.
            var facingIntent = _lastFacingIntentMem.ValueOr(moveVecDir);

            if (isMovingIntended)
            {
                var facingErrAngle
                    = Vector3.SignedAngle(curFacing.AsXZ(), moveVecDir.AsXZ(), Vector3.up);
                if (facingErrAngle.Abs() > 179.1f)
                {
                    curFacing = (Quaternion.AngleAxis(1f, Vector3.up) * curFacing.AsXZ()).xz();
                }

                facingIntent         = moveVecDir;
                _lastFacingIntentMem = moveVecDir;
            }
            curFacing = curFacing.SlerpedTo(facingIntent, turnSpeed * Time.deltaTime);
            this.transform.SetForward(curFacing.AsXZ());

            // Animation.
            if (curSpdXZ > 0.5f)
            {
                poser.curPoseIdx = poseIdx_dash;
            }
            else
            {
                poser.curPoseIdx = poseIdx_stand;
            }
        }