TwoBoneIK.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 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.Samples.Jobs
  6. {
  7. /// <summary>
  8. /// An sample of how to use Animation Jobs in Animancer to apply simple two bone Inverse Kinematics,
  9. /// even to Generic Rigs which are not supported by Unity's inbuilt IK system.
  10. /// </summary>
  11. ///
  12. /// <remarks>
  13. /// This sample 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 sample.
  26. /// <para></para>
  27. /// <strong>Sample:</strong>
  28. /// <see href="https://kybernetik.com.au/animancer/docs/samples/jobs/two-bone-ik">
  29. /// Two Bone IK</see>
  30. /// </remarks>
  31. ///
  32. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.Jobs/TwoBoneIK
  33. ///
  34. [AddComponentMenu(Strings.SamplesMenuPrefix + "Jobs - Two Bone IK")]
  35. [AnimancerHelpUrl(typeof(TwoBoneIK))]
  36. public class TwoBoneIK : MonoBehaviour
  37. {
  38. /************************************************************************************************************************/
  39. [SerializeField] private AnimancerComponent _Animancer;
  40. [SerializeField] private Transform _EndBone;
  41. [SerializeField] private Transform _Target;
  42. /************************************************************************************************************************/
  43. protected virtual void Awake()
  44. {
  45. // Get the bones we want to affect.
  46. Transform midBone = _EndBone.parent;
  47. Transform topBone = midBone.parent;
  48. // Create the job and setup its details.
  49. TwoBoneIKJob twoBoneIKJob = new();
  50. twoBoneIKJob.Setup(_Animancer.Animator, topBone, midBone, _EndBone, _Target);
  51. // Add it to Animancer's output.
  52. _Animancer.Graph.InsertOutputJob(twoBoneIKJob);
  53. }
  54. /************************************************************************************************************************/
  55. }
  56. }