BasicCharacterAnimations.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. /// Combines <see cref="BasicMovementAnimations"/> and <see cref="PlayTransitionOnClick"/> into one script.
  8. /// </summary>
  9. ///
  10. /// <remarks>
  11. /// <strong>Sample:</strong>
  12. /// <see href="https://kybernetik.com.au/animancer/docs/samples/basics/character">
  13. /// Basic Character</see>
  14. /// </remarks>
  15. ///
  16. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.Basics/BasicCharacterAnimations
  17. ///
  18. [AddComponentMenu(Strings.SamplesMenuPrefix + "Basics - Basic Character Animations")]
  19. [AnimancerHelpUrl(typeof(BasicCharacterAnimations))]
  20. public class BasicCharacterAnimations : MonoBehaviour
  21. {
  22. /************************************************************************************************************************/
  23. [SerializeField] private AnimancerComponent _Animancer;
  24. [SerializeField] private ClipTransition _Idle;
  25. [SerializeField] private ClipTransition _Move;
  26. [SerializeField] private ClipTransition _Action;
  27. private State _CurrentState;
  28. private enum State
  29. {
  30. NotActing,// Idle and Move can be interrupted.
  31. Acting,// Action can only be interrupted by itself.
  32. }
  33. /************************************************************************************************************************/
  34. protected virtual void Awake()
  35. {
  36. _Action.Events.OnEnd = UpdateMovement;
  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. _Animancer.Play(_Action);
  73. }
  74. }
  75. /************************************************************************************************************************/
  76. }
  77. }