Golfer.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.Events
  6. {
  7. /// <summary>Manages a character with the ability to hit a golf ball.</summary>
  8. /// <example><see href="https://kybernetik.com.au/animancer/docs/examples/events/golf">Golf Events</see></example>
  9. /// https://kybernetik.com.au/animancer/api/Animancer.Examples.Events/Golfer
  10. ///
  11. [AddComponentMenu(Strings.ExamplesMenuPrefix + "Golf Events - Golfer")]
  12. [HelpURL(Strings.DocsURLs.ExampleAPIDocumentation + nameof(Events) + "/" + nameof(Golfer))]
  13. public sealed class Golfer : MonoBehaviour
  14. {
  15. /************************************************************************************************************************/
  16. private const string HitEventName = "Hit";
  17. [SerializeField] private AnimancerComponent _Animancer;
  18. [SerializeField] private ClipTransition _Ready;
  19. [SerializeField, EventNames(HitEventName)] private ClipTransition _Swing;
  20. [SerializeField] private Rigidbody _Ball;
  21. [SerializeField] private Vector3 _HitVelocity = new Vector3(0, 10, 10);
  22. [SerializeField, Meters] private float _BallReturnHeight = -10;
  23. private Vector3 _BallStartPosition;
  24. /************************************************************************************************************************/
  25. private void Awake()
  26. {
  27. _BallStartPosition = _Ball.position;
  28. _Ball.isKinematic = true;
  29. _Swing.Events.SetCallback(HitEventName, HitBall);
  30. _Swing.Events.OnEnd = EndSwing;
  31. }
  32. /************************************************************************************************************************/
  33. private void OnEnable()
  34. {
  35. _Animancer.Play(_Ready);
  36. ResetBall();
  37. // Awake only gets called once on startup but OnEnable is called every time the object is activated.
  38. // It doesn't matter in the Golf Events example, but the Hybrid Mini Game example reuses this script and
  39. // deactivates it while the Mini Game is not being played so we want to always enter the ready state when
  40. // the Mini Game starts.
  41. }
  42. /************************************************************************************************************************/
  43. private void ResetBall()
  44. {
  45. _Ball.isKinematic = true;
  46. _Ball.position = _BallStartPosition;
  47. }
  48. /************************************************************************************************************************/
  49. private void Update()
  50. {
  51. if (_Ball.isKinematic)
  52. {
  53. if (ExampleInput.LeftMouseDown)
  54. {
  55. _Animancer.Play(_Swing);
  56. }
  57. }
  58. else if (_Ball.position.y < _BallReturnHeight)
  59. {
  60. ResetBall();
  61. }
  62. }
  63. /************************************************************************************************************************/
  64. private void HitBall()
  65. {
  66. _Ball.isKinematic = false;
  67. _Ball.velocity = _HitVelocity;
  68. }
  69. /************************************************************************************************************************/
  70. private void EndSwing()
  71. {
  72. // Since the swing animation is ending early, we want it to calculate the fade duration to fade out over
  73. // the remainder of that animation instead of just using the value specified by the _Ready transition.
  74. var fadeDuration = AnimancerEvent.GetFadeOutDuration();
  75. _Animancer.Play(_Ready, fadeDuration);
  76. }
  77. /************************************************************************************************************************/
  78. }
  79. }