TwoBoneIK.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. // Compare to the original script: https://github.com/Unity-Technologies/animation-jobs-samples/blob/master/Assets/animation-jobs-samples/Samples/Scripts/TwoBoneIK/TwoBoneIK.cs
  3. #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value.
  4. using UnityEngine;
  5. namespace Animancer.Examples.Jobs
  6. {
  7. /// <summary>
  8. /// An example of how to use Animation Jobs in Animancer to apply simple two bone Inverse Kinematics, even to
  9. /// Generic Rigs which are not supported by Unity's inbuilt IK system.
  10. /// </summary>
  11. ///
  12. /// <remarks>
  13. /// This example is based on Unity's
  14. /// <see href="https://github.com/Unity-Technologies/animation-jobs-samples">Animation Jobs Samples</see>.
  15. /// <para></para>
  16. /// This script sets up the job in place of
  17. /// <see href="https://github.com/Unity-Technologies/animation-jobs-samples/blob/master/Assets/animation-jobs-samples/Samples/Scripts/TwoBoneIK/TwoBoneIK.cs">
  18. /// TwoBoneIK.cs</see>.
  19. /// <para></para>
  20. /// The <see cref="TwoBoneIKJob"/> script is almost identical to the original
  21. /// <see href="https://github.com/Unity-Technologies/animation-jobs-samples/blob/master/Assets/animation-jobs-samples/Runtime/AnimationJobs/TwoBoneIKJob.cs">
  22. /// TwoBoneIKJob.cs</see>.
  23. /// <para></para>
  24. /// The <see href="https://learn.unity.com/tutorial/working-with-animation-rigging">Animation Rigging</see> package
  25. /// has an IK system which is much better than this example.
  26. /// </remarks>
  27. ///
  28. /// <example><see href="https://kybernetik.com.au/animancer/docs/examples/jobs/two-bone-ik">Two Bone IK</see></example>
  29. ///
  30. /// https://kybernetik.com.au/animancer/api/Animancer.Examples.Jobs/TwoBoneIK
  31. ///
  32. [AddComponentMenu(Strings.ExamplesMenuPrefix + "Jobs - Two Bone IK")]
  33. [HelpURL(Strings.DocsURLs.ExampleAPIDocumentation + nameof(Jobs) + "/" + nameof(TwoBoneIK))]
  34. public sealed class TwoBoneIK : MonoBehaviour
  35. {
  36. /************************************************************************************************************************/
  37. [SerializeField] private AnimancerComponent _Animancer;
  38. [SerializeField] private Transform _EndBone;
  39. [SerializeField] private Transform _Target;
  40. /************************************************************************************************************************/
  41. private void Awake()
  42. {
  43. // Get the bones we want to affect.
  44. var midBone = _EndBone.parent;
  45. var topBone = midBone.parent;
  46. // Create the job and setup its details.
  47. var twoBoneIKJob = new TwoBoneIKJob();
  48. twoBoneIKJob.Setup(_Animancer.Animator, topBone, midBone, _EndBone, _Target);
  49. // Add it to Animancer's output.
  50. _Animancer.Playable.InsertOutputJob(twoBoneIKJob);
  51. }
  52. /************************************************************************************************************************/
  53. }
  54. }