BasicCharacterAnimations.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value.
  3. using UnityEngine;
  4. namespace Animancer.Examples.Basics
  5. {
  6. /// <summary>
  7. /// Combines <see cref="BasicMovementAnimations"/> and <see cref="PlayTransitionOnClick"/> into one script.
  8. /// </summary>
  9. /// <example><see href="https://kybernetik.com.au/animancer/docs/examples/basics/character">Basic Character</see></example>
  10. /// https://kybernetik.com.au/animancer/api/Animancer.Examples.Basics/BasicCharacterAnimations
  11. ///
  12. [AddComponentMenu(Strings.ExamplesMenuPrefix + "Basics - Basic Character Animations")]
  13. [HelpURL(Strings.DocsURLs.ExampleAPIDocumentation + nameof(Basics) + "/" + nameof(BasicCharacterAnimations))]
  14. public sealed class BasicCharacterAnimations : MonoBehaviour
  15. {
  16. /************************************************************************************************************************/
  17. [SerializeField] private AnimancerComponent _Animancer;
  18. [SerializeField] private ClipTransition _Idle;
  19. [SerializeField] private ClipTransition _Move;
  20. [SerializeField] private ClipTransition _Action;
  21. private enum State
  22. {
  23. /// <summary><see cref="_Idle"/> or <see cref="_Move"/>.</summary>
  24. NotActing,
  25. /// <summary><see cref="_Action"/>.</summary>
  26. Acting,
  27. }
  28. private State _CurrentState;
  29. /************************************************************************************************************************/
  30. private void Awake()
  31. {
  32. _Action.Events.OnEnd = OnActionEnd;
  33. }
  34. /************************************************************************************************************************/
  35. private void OnActionEnd()
  36. {
  37. _CurrentState = State.NotActing;
  38. UpdateMovement();
  39. }
  40. /************************************************************************************************************************/
  41. private void Update()
  42. {
  43. switch (_CurrentState)
  44. {
  45. case State.NotActing:
  46. UpdateMovement();
  47. UpdateAction();
  48. break;
  49. case State.Acting:
  50. UpdateAction();
  51. break;
  52. }
  53. }
  54. /************************************************************************************************************************/
  55. private void UpdateMovement()
  56. {
  57. float forward = ExampleInput.WASD.y;
  58. if (forward > 0)
  59. {
  60. _Animancer.Play(_Move);
  61. }
  62. else
  63. {
  64. _Animancer.Play(_Idle);
  65. }
  66. }
  67. /************************************************************************************************************************/
  68. private void UpdateAction()
  69. {
  70. if (ExampleInput.LeftMouseUp)
  71. {
  72. _CurrentState = State.Acting;
  73. _Animancer.Play(_Action);
  74. }
  75. }
  76. /************************************************************************************************************************/
  77. }
  78. }