HoldingHands.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using UnityEngine;
  2. using System.Collections;
  3. using RootMotion.FinalIK;
  4. namespace RootMotion.Demos {
  5. /// <summary>
  6. /// Holding hands rig.
  7. /// </summary>
  8. public class HoldingHands : MonoBehaviour {
  9. public FullBodyBipedIK rightHandChar, leftHandChar; // The characters
  10. public Transform rightHandTarget, leftHandTarget; // IK targets for the hands
  11. public float crossFade; // Which character is dominating?
  12. public float speed = 10f; // Speed of smoothly lerping the hands target
  13. private Quaternion rightHandRotation, leftHandRotation;
  14. void Start() {
  15. // Find the rotations of the hands target (this gameobject) in the rotation spaces of the hand bones
  16. rightHandRotation = Quaternion.Inverse(rightHandChar.solver.rightHandEffector.bone.rotation) * transform.rotation;
  17. leftHandRotation = Quaternion.Inverse(leftHandChar.solver.leftHandEffector.bone.rotation) * transform.rotation;
  18. }
  19. void LateUpdate () {
  20. // Positioning the hands target
  21. Vector3 targetPosition = Vector3.Lerp(rightHandChar.solver.rightHandEffector.bone.position, leftHandChar.solver.leftHandEffector.bone.position, crossFade);
  22. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * speed);
  23. // Rotating the hands target
  24. transform.rotation = Quaternion.Slerp(rightHandChar.solver.rightHandEffector.bone.rotation * rightHandRotation, leftHandChar.solver.leftHandEffector.bone.rotation * leftHandRotation, crossFade);
  25. // Set effector positions and rotations
  26. rightHandChar.solver.rightHandEffector.position = rightHandTarget.position;
  27. rightHandChar.solver.rightHandEffector.rotation = rightHandTarget.rotation;
  28. leftHandChar.solver.leftHandEffector.position = leftHandTarget.position;
  29. leftHandChar.solver.leftHandEffector.rotation = leftHandTarget.rotation;
  30. }
  31. }
  32. }