LayeredCharacterAnimations.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 Animancer.Units;
  4. using UnityEngine;
  5. namespace Animancer.Examples.Layers
  6. {
  7. /// <summary>Demonstrates how to use layers to play multiple animations at once on different body parts.</summary>
  8. /// <example><see href="https://kybernetik.com.au/animancer/docs/examples/layers">Layers</see></example>
  9. /// https://kybernetik.com.au/animancer/api/Animancer.Examples.Layers/LayeredCharacterAnimations
  10. ///
  11. [AddComponentMenu(Strings.ExamplesMenuPrefix + "Layers - Layered Character Animations")]
  12. [HelpURL(Strings.DocsURLs.ExampleAPIDocumentation + nameof(Layers) + "/" + nameof(LayeredCharacterAnimations))]
  13. public sealed class LayeredCharacterAnimations : MonoBehaviour
  14. {
  15. /************************************************************************************************************************/
  16. [SerializeField] private AnimancerComponent _Animancer;
  17. [SerializeField] private ClipTransition _Idle;
  18. [SerializeField] private ClipTransition _Move;
  19. [SerializeField] private ClipTransition _Action;
  20. [SerializeField] private AvatarMask _ActionMask;
  21. [SerializeField, Seconds] private float _ActionFadeOutDuration = AnimancerPlayable.DefaultFadeDuration;
  22. /************************************************************************************************************************/
  23. private AnimancerLayer _BaseLayer;
  24. private AnimancerLayer _ActionLayer;
  25. /************************************************************************************************************************/
  26. private void Awake()
  27. {
  28. _BaseLayer = _Animancer.Layers[0];
  29. _ActionLayer = _Animancer.Layers[1];// First access to a layer creates it.
  30. _ActionLayer.SetMask(_ActionMask);
  31. _ActionLayer.SetDebugName("Action Layer");
  32. _Action.Events.OnEnd = OnActionEnd;
  33. }
  34. /************************************************************************************************************************/
  35. private void Update()
  36. {
  37. UpdateMovement();
  38. UpdateAction();
  39. }
  40. /************************************************************************************************************************/
  41. private void UpdateMovement()
  42. {
  43. float forward = ExampleInput.WASD.y;
  44. if (forward > 0)
  45. {
  46. _BaseLayer.Play(_Move);
  47. }
  48. else
  49. {
  50. _BaseLayer.Play(_Idle);
  51. }
  52. }
  53. /************************************************************************************************************************/
  54. private void UpdateAction()
  55. {
  56. if (ExampleInput.LeftMouseUp)
  57. {
  58. _ActionLayer.Play(_Action);
  59. }
  60. }
  61. /************************************************************************************************************************/
  62. private void OnActionEnd()
  63. {
  64. _ActionLayer.StartFade(0, _ActionFadeOutDuration);
  65. }
  66. /************************************************************************************************************************/
  67. }
  68. }