void LateUpdate() { RightHandMatrix = ChangeCoordinateSystems(HandR, Gun); LeftHandMatrix = ChangeCoordinateSystems(HandL, Gun); Gun.LookAt(AimPoint.transform.position); Gun.transform.Rotate(Vector3.up, 90); //Right arm Vector3 desiredRightWristPosition = (Gun.localToWorldMatrix * RightHandMatrix).MultiplyPoint3x4(Vector3.zero); Transform[] bonesR = new Transform[3]; bonesR[0] = ArmR; bonesR[1] = ForearmR; bonesR[2] = HandR; IKSolver.Solve(bonesR, desiredRightWristPosition); HandR.rotation = QuatFromMatrix(Gun.localToWorldMatrix * RightHandMatrix); //Left arm Vector3 desiredLeftWristPosition = (Gun.localToWorldMatrix * LeftHandMatrix).MultiplyPoint3x4(Vector3.zero); Transform[] bonesL = new Transform[3]; bonesL[0] = ArmL; bonesL[1] = ForearmL; bonesL[2] = HandL; IKSolver.Solve(bonesL, desiredLeftWristPosition); HandL.rotation = QuatFromMatrix(Gun.localToWorldMatrix * RightHandMatrix); }
// LateUpdate is called once per frame, after animation sampling public override void LateUpdateCustom() { // it's important to do weapon snapping first, because we do hand snapping to weapon after that if (weaponAdjustment > 0) { Matrix4x4 weaponMatrix = legSnap.localToWorldMatrix * weaponRelativeToLeg; aimWeapon.rotation = Quaternion.Lerp(aimWeapon.rotation, Util.QuaternionFromMatrix(weaponMatrix), weaponAdjustment); aimWeapon.position = Vector3.Lerp(aimWeapon.position, weaponMatrix.GetColumn(3), weaponAdjustment); } for (int arm = 0; arm < arms.Length; arm++) { // Find out how much IK adjustment to use float adjustment = 1; if (arm == 0) { adjustment = arm0adjustment; } if (arm == 1) { adjustment = arm1adjustment; } // Don't adjust if no adjustment if (adjustment <= 0) { continue; } // Remember original arm bone rotations Quaternion origShoulder = arms[arm].shoulder.rotation; Quaternion origElbow = arms[arm].elbow.rotation; Quaternion origWrist = arms[arm].wrist.rotation; // IK to make wrist go into desired position Vector3 desiredWristPosition = (aimWeapon.localToWorldMatrix * arms[arm].handRelativeToGun).MultiplyPoint3x4(Vector3.zero); ikSolver.Solve(new Transform[] { arms[arm].shoulder, arms[arm].elbow, arms[arm].wrist }, desiredWristPosition); // Get adjusted wrist rotation arms[arm].wrist.rotation = Util.QuaternionFromMatrix(aimWeapon.localToWorldMatrix * arms[arm].handRelativeToGun); // Lerp between orig and adjusted rotations iff less than full adjustment if (adjustment < 1) { arms[arm].shoulder.rotation = Quaternion.Lerp(origShoulder, arms[arm].shoulder.rotation, adjustment); arms[arm].elbow.rotation = Quaternion.Lerp(origElbow, arms[arm].elbow.rotation, adjustment); arms[arm].wrist.rotation = Quaternion.Lerp(origWrist, arms[arm].wrist.rotation, adjustment); } } }