LibraryCharacterAnimations.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.Basics
  5. {
  6. /// <summary>
  7. /// Implements the same behaviour as <see cref="BasicCharacterAnimations"/>
  8. /// using <see cref="TransitionAsset"/>s.
  9. /// </summary>
  10. ///
  11. /// <remarks>
  12. /// <strong>Sample:</strong>
  13. /// <see href="https://kybernetik.com.au/animancer/docs/samples/basics/library">
  14. /// Library Basics</see>
  15. /// </remarks>
  16. ///
  17. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.Basics/LibraryCharacterAnimations
  18. ///
  19. [AddComponentMenu(Strings.SamplesMenuPrefix + "Basics - Library Character Animations")]
  20. [AnimancerHelpUrl(typeof(LibraryCharacterAnimations))]
  21. public class LibraryCharacterAnimations : MonoBehaviour
  22. {
  23. /************************************************************************************************************************/
  24. // This script is almost identical to BasicCharacterAnimations, with a few differences:
  25. // - It uses TransitionAssets instead of ClipTransitions.
  26. // - It assigns the Action state's End Event after playing it instead of on startup.
  27. /************************************************************************************************************************/
  28. [SerializeField] private AnimancerComponent _Animancer;
  29. [SerializeField] private TransitionAsset _Idle;
  30. [SerializeField] private TransitionAsset _Move;
  31. [SerializeField] private TransitionAsset _Action;
  32. private State _CurrentState;
  33. private enum State
  34. {
  35. NotActing,// Idle and Move can be interrupted.
  36. Acting,// Action can only be interrupted by itself.
  37. }
  38. /************************************************************************************************************************/
  39. protected virtual void Update()
  40. {
  41. switch (_CurrentState)
  42. {
  43. case State.NotActing:
  44. UpdateMovement();
  45. UpdateAction();
  46. break;
  47. case State.Acting:
  48. UpdateAction();
  49. break;
  50. }
  51. }
  52. /************************************************************************************************************************/
  53. private void UpdateMovement()
  54. {
  55. _CurrentState = State.NotActing;
  56. float forward = SampleInput.WASD.y;
  57. if (forward > 0)
  58. {
  59. _Animancer.Play(_Move);
  60. }
  61. else
  62. {
  63. _Animancer.Play(_Idle);
  64. }
  65. }
  66. /************************************************************************************************************************/
  67. private void UpdateAction()
  68. {
  69. if (SampleInput.LeftMouseUp)
  70. {
  71. _CurrentState = State.Acting;
  72. // _Action is an asset that could be shared by multiple different characters
  73. // as well as instances of the same character so we can't set up the events
  74. // in the transition because the characters would conflict with each other.
  75. // Instead, we add events to the state so each character's events are separate.
  76. AnimancerState state = _Animancer.Play(_Action);
  77. state.Events(this).OnEnd ??= UpdateMovement;
  78. }
  79. }
  80. /************************************************************************************************************************/
  81. }
  82. }