BipedIKvsAnimatorIK.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using UnityEngine;
  2. using System.Collections;
  3. using RootMotion.FinalIK;
  4. namespace RootMotion.Demos {
  5. /// <summary>
  6. /// Demo script that shows how BipedIK performs compared to the built-in Animator IK
  7. /// </summary>
  8. public class BipedIKvsAnimatorIK: MonoBehaviour {
  9. [LargeHeader("References")]
  10. public Animator animator;
  11. public BipedIK bipedIK;
  12. // Look At
  13. [LargeHeader("Look At")]
  14. public Transform lookAtTargetBiped;
  15. public Transform lookAtTargetAnimator;
  16. [Range(0f, 1f)] public float lookAtWeight = 1f;
  17. [Range(0f, 1f)] public float lookAtBodyWeight = 1f;
  18. [Range(0f, 1f)] public float lookAtHeadWeight = 1f;
  19. [Range(0f, 1f)] public float lookAtEyesWeight = 1f;
  20. [Range(0f, 1f)] public float lookAtClampWeight = 0.5f;
  21. [Range(0f, 1f)] public float lookAtClampWeightHead = 0.5f;
  22. [Range(0f, 1f)] public float lookAtClampWeightEyes = 0.5f;
  23. // Foot
  24. [LargeHeader("Foot")]
  25. public Transform footTargetBiped;
  26. public Transform footTargetAnimator;
  27. [Range(0f, 1f)] public float footPositionWeight = 0f;
  28. [Range(0f, 1f)] public float footRotationWeight = 0f;
  29. // Hand
  30. [LargeHeader("Hand")]
  31. public Transform handTargetBiped;
  32. public Transform handTargetAnimator;
  33. [Range(0f, 1f)] public float handPositionWeight = 0f;
  34. [Range(0f, 1f)] public float handRotationWeight = 0f;
  35. void OnAnimatorIK(int layer) {
  36. animator.transform.rotation = bipedIK.transform.rotation;
  37. Vector3 offset = animator.transform.position - bipedIK.transform.position;
  38. // Look At
  39. lookAtTargetAnimator.position = lookAtTargetBiped.position + offset;
  40. bipedIK.SetLookAtPosition(lookAtTargetBiped.position);
  41. bipedIK.SetLookAtWeight(lookAtWeight, lookAtBodyWeight, lookAtHeadWeight, lookAtEyesWeight, lookAtClampWeight, lookAtClampWeightHead, lookAtClampWeightEyes);
  42. animator.SetLookAtPosition(lookAtTargetAnimator.position);
  43. animator.SetLookAtWeight(lookAtWeight, lookAtBodyWeight, lookAtHeadWeight, lookAtEyesWeight, lookAtClampWeight);
  44. // Foot
  45. footTargetAnimator.position = footTargetBiped.position + offset;
  46. footTargetAnimator.rotation = footTargetBiped.rotation;
  47. bipedIK.SetIKPosition(AvatarIKGoal.LeftFoot, footTargetBiped.position);
  48. bipedIK.SetIKRotation(AvatarIKGoal.LeftFoot, footTargetBiped.rotation);
  49. bipedIK.SetIKPositionWeight(AvatarIKGoal.LeftFoot, footPositionWeight);
  50. bipedIK.SetIKRotationWeight(AvatarIKGoal.LeftFoot, footRotationWeight);
  51. animator.SetIKPosition(AvatarIKGoal.LeftFoot, footTargetAnimator.position);
  52. animator.SetIKRotation(AvatarIKGoal.LeftFoot, footTargetAnimator.rotation);
  53. animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, footPositionWeight);
  54. animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot, footRotationWeight);
  55. // Hand
  56. handTargetAnimator.position = handTargetBiped.position + offset;
  57. handTargetAnimator.rotation = handTargetBiped.rotation;
  58. bipedIK.SetIKPosition(AvatarIKGoal.LeftHand, handTargetBiped.position);
  59. bipedIK.SetIKRotation(AvatarIKGoal.LeftHand, handTargetBiped.rotation);
  60. bipedIK.SetIKPositionWeight(AvatarIKGoal.LeftHand, handPositionWeight);
  61. bipedIK.SetIKRotationWeight(AvatarIKGoal.LeftHand, handRotationWeight);
  62. animator.SetIKPosition(AvatarIKGoal.LeftHand, handTargetAnimator.position);
  63. animator.SetIKRotation(AvatarIKGoal.LeftHand, handTargetAnimator.rotation);
  64. animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, handPositionWeight);
  65. animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, handRotationWeight);
  66. }
  67. }
  68. }