DirectionalBasics.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.Sprites
  5. {
  6. /// <summary>
  7. /// Animates a character to either stand idle or walk using animations
  8. /// defined in <see cref="DirectionalAnimationSet"/>s.
  9. /// </summary>
  10. ///
  11. /// <remarks>
  12. /// <strong>Sample:</strong>
  13. /// <see href="https://kybernetik.com.au/animancer/docs/samples/sprites/basics">
  14. /// Directional Basics</see>
  15. /// </remarks>
  16. ///
  17. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.Sprites/DirectionalBasics
  18. ///
  19. [AddComponentMenu(Strings.SamplesMenuPrefix + "Sprites - Directional Basics")]
  20. [AnimancerHelpUrl(typeof(DirectionalBasics))]
  21. public class DirectionalBasics : MonoBehaviour
  22. {
  23. /************************************************************************************************************************/
  24. [SerializeField] private AnimancerComponent _Animancer;
  25. [SerializeField] private DirectionalAnimationSet _Idles;
  26. [SerializeField] private DirectionalAnimationSet _Walks;
  27. [SerializeField] private Vector2 _Facing = Vector2.down;
  28. /************************************************************************************************************************/
  29. protected virtual void Update()
  30. {
  31. Vector2 input = SampleInput.WASD;
  32. if (input != Vector2.zero)
  33. {
  34. _Facing = input;
  35. Play(_Walks);
  36. // Play could return the AnimancerState it gets from _Animancer.Play,
  37. // But we can also just access it using _Animancer.States.Current.
  38. bool isRunning = SampleInput.LeftShiftHold;
  39. _Animancer.States.Current.Speed = isRunning ? 2 : 1;
  40. }
  41. else
  42. {
  43. // When we're not moving, we still remember the direction we're facing
  44. // so we can continue using the correct idle animation for that direction.
  45. Play(_Idles);
  46. }
  47. }
  48. /************************************************************************************************************************/
  49. private void Play(DirectionalAnimationSet animations)
  50. {
  51. // Instead of only a single animation, we have a different one for each direction we can face.
  52. // So we get whichever is appropriate for that direction and play it.
  53. AnimationClip clip = animations.GetClip(_Facing);
  54. _Animancer.Play(clip);
  55. // Or we could do that in one line:
  56. // _Animancer.Play(animations.GetClip(_Facing));
  57. }
  58. /************************************************************************************************************************/
  59. #if UNITY_EDITOR
  60. /************************************************************************************************************************/
  61. /// <summary>[Editor-Only]
  62. /// Sets the character's starting sprite in Edit Mode so you can see it while working in the scene.
  63. /// </summary>
  64. protected virtual void OnValidate()
  65. {
  66. if (_Idles != null)
  67. _Idles.GetClip(_Facing).EditModeSampleAnimation(_Animancer);
  68. }
  69. /************************************************************************************************************************/
  70. #endif
  71. /************************************************************************************************************************/
  72. }
  73. }