LayeredCharacterAnimations.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 Animancer.Units;
  4. using UnityEngine;
  5. namespace Animancer.Samples.Layers
  6. {
  7. /// <summary>
  8. /// Demonstrates how to use layers to play multiple
  9. /// independent animations at the same time on different body parts.
  10. /// </summary>
  11. ///
  12. /// <remarks>
  13. /// <strong>Sample:</strong>
  14. /// <see href="https://kybernetik.com.au/animancer/docs/samples/layers/basic">
  15. /// Basic Layers</see>
  16. /// </remarks>
  17. ///
  18. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.Layers/LayeredCharacterAnimations
  19. ///
  20. [AddComponentMenu(Strings.SamplesMenuPrefix + "Layers - Layered Character Animations")]
  21. [AnimancerHelpUrl(typeof(LayeredCharacterAnimations))]
  22. public class LayeredCharacterAnimations : MonoBehaviour
  23. {
  24. /************************************************************************************************************************/
  25. [SerializeField] private AnimancerComponent _Animancer;
  26. [SerializeField] private ClipTransition _Idle;
  27. [SerializeField] private ClipTransition _Move;
  28. [SerializeField] private ClipTransition _Action;
  29. [SerializeField] private AvatarMask _ActionMask;
  30. [SerializeField, Seconds] private float _ActionFadeOutDuration = AnimancerGraph.DefaultFadeDuration;
  31. /************************************************************************************************************************/
  32. private AnimancerLayer _BaseLayer;
  33. private AnimancerLayer _ActionLayer;
  34. /************************************************************************************************************************/
  35. protected virtual void Awake()
  36. {
  37. _BaseLayer = _Animancer.Layers[0];
  38. _ActionLayer = _Animancer.Layers[1];// First access to a layer creates it.
  39. _ActionLayer.Mask = _ActionMask;
  40. _ActionLayer.SetDebugName("Action Layer");
  41. _Action.Events.OnEnd = OnActionEnd;
  42. }
  43. /************************************************************************************************************************/
  44. protected virtual void Update()
  45. {
  46. UpdateMovement();
  47. UpdateAction();
  48. }
  49. /************************************************************************************************************************/
  50. private void UpdateMovement()
  51. {
  52. float forward = SampleInput.WASD.y;
  53. if (forward > 0)
  54. {
  55. _BaseLayer.Play(_Move);
  56. }
  57. else
  58. {
  59. _BaseLayer.Play(_Idle);
  60. }
  61. }
  62. /************************************************************************************************************************/
  63. private void UpdateAction()
  64. {
  65. if (SampleInput.LeftMouseUp)
  66. {
  67. _ActionLayer.Play(_Action);
  68. }
  69. }
  70. /************************************************************************************************************************/
  71. private void OnActionEnd()
  72. {
  73. _ActionLayer.StartFade(0, _ActionFadeOutDuration);
  74. }
  75. /************************************************************************************************************************/
  76. }
  77. }