HybridCharacterAnimations.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.AnimatorControllers
  5. {
  6. /// <summary>
  7. /// Implements the same behaviour as <see cref="BasicCharacterAnimations"/>
  8. /// using a <see cref="HybridAnimancerComponent"/>.
  9. /// </summary>
  10. ///
  11. /// <remarks>
  12. /// <strong>Sample:</strong>
  13. /// <see href="https://kybernetik.com.au/animancer/docs/samples/animator-controllers/character">
  14. /// Hybrid Character</see>
  15. /// </remarks>
  16. ///
  17. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.AnimatorControllers/HybridCharacterAnimations
  18. ///
  19. [AddComponentMenu(Strings.SamplesMenuPrefix + "Animator Controllers - Hybrid Character Animations")]
  20. [AnimancerHelpUrl(typeof(HybridCharacterAnimations))]
  21. // Awake before Animancer to disable the OptionalWarnings before it triggers them.
  22. [DefaultExecutionOrder(AnimancerComponent.DefaultExecutionOrder - 1000)]
  23. public class HybridCharacterAnimations : MonoBehaviour
  24. {
  25. /************************************************************************************************************************/
  26. public static readonly int IsMovingParameter = Animator.StringToHash("IsMoving");
  27. [SerializeField] private HybridAnimancerComponent _Animancer;
  28. [SerializeField] private ClipTransition _Action;
  29. private State _CurrentState;
  30. private enum State
  31. {
  32. NotActing,// Idle and Move can be interrupted.
  33. Acting,// Action can only be interrupted by itself.
  34. }
  35. /************************************************************************************************************************/
  36. protected virtual void Awake()
  37. {
  38. _Action.Events.OnEnd = UpdateMovement;
  39. // This sample's documentation explains why these warnings exist so we don't need them enabled.
  40. OptionalWarning.NativeControllerHumanoid.Disable();
  41. OptionalWarning.NativeControllerHybrid.Disable();
  42. }
  43. /************************************************************************************************************************/
  44. protected virtual void Update()
  45. {
  46. switch (_CurrentState)
  47. {
  48. case State.NotActing:
  49. UpdateMovement();
  50. UpdateAction();
  51. break;
  52. case State.Acting:
  53. UpdateAction();
  54. break;
  55. }
  56. }
  57. /************************************************************************************************************************/
  58. private void UpdateMovement()
  59. {
  60. _CurrentState = State.NotActing;
  61. float forward = SampleInput.WASD.y;
  62. bool isMoving = forward > 0;
  63. // This sample script demonstrates both the Native and Hybrid approaches.
  64. // In a real project you would only use one system or the other, not both.
  65. // Native - Animator Controller assigned to the Animator.
  66. // In this case, the HybridAnimancerComponent is unnecessary and you should use a base AnimancerComponent.
  67. if (_Animancer.Animator.runtimeAnimatorController != null)
  68. {
  69. if (_Animancer.Controller.Controller != null)
  70. {
  71. _Animancer.Controller.Controller = null;
  72. Debug.LogWarning(
  73. $"A Native Animator Controller is assigned to the Animator component" +
  74. $" and a Hybrid Animator Controller is also assigned to the {nameof(HybridAnimancerComponent)}." +
  75. $" That's not necessarily a problem, but using both systems at the same time is very unusual.",
  76. this);
  77. }
  78. // Return to the Animator Controller by fading out Animancer's layers.
  79. AnimancerLayer layer = _Animancer.Layers[0];
  80. if (layer.TargetWeight > 0)
  81. layer.StartFade(0, 0.25f);
  82. // Set parameters on the Animator compponent.
  83. _Animancer.Animator.SetBool(IsMovingParameter, isMoving);
  84. }
  85. // Hybrid - Animator Controller assigned to the HybridAnimancerComponent.
  86. // In this case, the Animator component doesn't have a reference to the Animator Controller.
  87. else if (_Animancer.Controller.Controller != null)
  88. {
  89. // Return to the Animator Controller by playing the ControllerTransition.
  90. _Animancer.PlayController();
  91. // Set parameters on the ControllerState.
  92. _Animancer.SetBool(IsMovingParameter, isMoving);
  93. }
  94. else
  95. {
  96. Debug.LogError("No Animator Controller is assigned.", this);
  97. }
  98. }
  99. /************************************************************************************************************************/
  100. private void UpdateAction()
  101. {
  102. if (SampleInput.LeftMouseUp)
  103. {
  104. _CurrentState = State.Acting;
  105. _Animancer.Play(_Action);
  106. }
  107. }
  108. /************************************************************************************************************************/
  109. }
  110. }