IKPuppet.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value.
  3. using UnityEngine;
  4. namespace Animancer.Samples.InverseKinematics
  5. {
  6. /// <summary>Demonstrates how to use Unity's Inverse Kinematics (IK) system to move a character's limbs.</summary>
  7. ///
  8. /// <remarks>
  9. /// <strong>Sample:</strong>
  10. /// <see href="https://kybernetik.com.au/animancer/docs/samples/ik/puppet">
  11. /// Puppet</see>
  12. /// </remarks>
  13. ///
  14. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.InverseKinematics/IKPuppet
  15. ///
  16. [AddComponentMenu(Strings.SamplesMenuPrefix + "Inverse Kinematics - IK Puppet")]
  17. [AnimancerHelpUrl(typeof(IKPuppet))]
  18. public class IKPuppet : MonoBehaviour
  19. {
  20. /************************************************************************************************************************/
  21. [SerializeField] private AnimancerComponent _Animancer;
  22. [SerializeField] private Transform _BodyTarget;
  23. [SerializeField] private IKPuppetLookTarget _LookTarget;
  24. [SerializeField] private IKPuppetTarget[] _IKTargets;
  25. /************************************************************************************************************************/
  26. protected virtual void Awake()
  27. {
  28. // Tell Unity that we want it to call OnAnimatorIK for states on this layer:
  29. _Animancer.Layers[0].ApplyAnimatorIK = true;
  30. }
  31. /************************************************************************************************************************/
  32. protected virtual void OnAnimatorIK(int layerIndex)
  33. {
  34. _Animancer.Animator.bodyPosition = _BodyTarget.position;
  35. _Animancer.Animator.bodyRotation = _BodyTarget.rotation;
  36. _LookTarget.UpdateAnimatorIK(_Animancer.Animator);
  37. for (int i = 0; i < _IKTargets.Length; i++)
  38. {
  39. _IKTargets[i].UpdateAnimatorIK(_Animancer.Animator);
  40. }
  41. }
  42. /************************************************************************************************************************/
  43. }
  44. }