Example #1
0
        // Positioning the left hand on the gun after aiming has finished
        private void FBBIK()
        {
            // Store the current rotation of the right hand
            rightHandRotation = ik.references.rightHand.rotation;

            // Offsetting hands, you might need that to support multiple weapons with the same aiming pose
            Vector3 rightHandOffset = ik.references.rightHand.rotation * gunHoldOffset;

            ik.solver.rightHandEffector.positionOffset += rightHandOffset;

            if (recoil != null)
            {
                recoil.SetHandRotations(rightHandRotation * leftHandRotRelToRightHand, rightHandRotation);
            }

            // Update FBBIK
            ik.solver.Update();

            // Rotating the hand bones after IK has finished
            // IK计算完之后再写入 recoil 的 IK计算
            if (recoil != null)
            {
                ik.references.rightHand.rotation = recoil.rotationOffset * rightHandRotation;
                ik.references.leftHand.rotation  = recoil.rotationOffset * rightHandRotation * leftHandRotRelToRightHand;
            }
            else
            {
                ik.references.rightHand.rotation = rightHandRotation;
                ik.references.leftHand.rotation  = rightHandRotation * leftHandRotRelToRightHand;
            }
        }
Example #2
0
        private void LookDownTheSight()
        {
            float sW = aimWeight * sightWeight;

            //if (sW <= 0f && recoil == null) return;

            // Interpolate the gunTarget from the current animated position of the gun to the position fixed to the camera
            gunTarget.position = Vector3.Lerp(gun.position, gunTarget.parent.TransformPoint(gunTargetDefaultLocalPosition), sW);
            gunTarget.rotation = Quaternion.Lerp(gun.rotation, gunTarget.parent.rotation * gunTargetDefaultLocalRotation, sW);

            // Get the current positions of the hands relative to the gun
            Vector3 leftHandRelativePosition  = gun.InverseTransformPoint(ik.solver.leftHandEffector.bone.position);
            Vector3 rightHandRelativePosition = gun.InverseTransformPoint(ik.solver.rightHandEffector.bone.position);

            // Get the current rotations of the hands relative to the gun
            Quaternion leftHandRelativeRotation  = Quaternion.Inverse(gun.rotation) * ik.solver.leftHandEffector.bone.rotation;
            Quaternion rightHandRelativeRotation = Quaternion.Inverse(gun.rotation) * ik.solver.rightHandEffector.bone.rotation;

            // Position the hands to the gun target the same way they are positioned on the gun
            //ik.solver.leftHandEffector.position = gunTarget.TransformPoint(leftHandRelativePosition);
            //ik.solver.rightHandEffector.position = gunTarget.TransformPoint(rightHandRelativePosition);

            //float handWeight = aimWeight > 0 && sightWeight > 0? aimWeight * sightWeight: 0f;
            float handWeight = 1f;            //aimWeight * sightWeight;

            ik.solver.leftHandEffector.positionOffset  += (gunTarget.TransformPoint(leftHandRelativePosition) - (ik.solver.leftHandEffector.bone.position + ik.solver.leftHandEffector.positionOffset)) * handWeight;
            ik.solver.rightHandEffector.positionOffset += (gunTarget.TransformPoint(rightHandRelativePosition) - (ik.solver.rightHandEffector.bone.position + ik.solver.rightHandEffector.positionOffset)) * handWeight;

            // Make sure the head does not rotate
            ik.solver.headMapping.maintainRotationWeight = 1f;

            if (recoil != null)
            {
                recoil.SetHandRotations(gunTarget.rotation * leftHandRelativeRotation, gunTarget.rotation * rightHandRelativeRotation);
            }

            // Update FBBIK
            ik.solver.Update();

            // Rotate the hand bones relative to the gun target the same way they are rotated relative to the gun
            if (recoil != null)
            {
                ik.references.leftHand.rotation  = recoil.rotationOffset * (gunTarget.rotation * leftHandRelativeRotation);
                ik.references.rightHand.rotation = recoil.rotationOffset * (gunTarget.rotation * rightHandRelativeRotation);
            }
            else
            {
                ik.references.leftHand.rotation  = gunTarget.rotation * leftHandRelativeRotation;
                ik.references.rightHand.rotation = gunTarget.rotation * rightHandRelativeRotation;
            }

            // Position the camera to where it was before FBBIK relative to the gun
            cam.transform.position = Vector3.Lerp(cam.transform.position, Vector3.Lerp(gunTarget.TransformPoint(camRelativeToGunTarget), gun.transform.TransformPoint(camRelativeToGunTarget), cameraRecoilWeight), sW);
        }