GolfBall.cs 3.2 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 Animancer.Units;
  4. using UnityEngine;
  5. namespace Animancer.Samples.Events
  6. {
  7. /// <summary>Manages the physics of a golf ball.</summary>
  8. ///
  9. /// <remarks>
  10. /// <strong>Sample:</strong>
  11. /// <see href="https://kybernetik.com.au/animancer/docs/samples/events/golf">
  12. /// Golf Events</see>
  13. /// </remarks>
  14. ///
  15. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.Events/GolfBall
  16. ///
  17. [AddComponentMenu(Strings.SamplesMenuPrefix + "Golf Events - Golf Ball")]
  18. [AnimancerHelpUrl(typeof(GolfBall))]
  19. public class GolfBall : MonoBehaviour
  20. {
  21. /************************************************************************************************************************/
  22. #if UNITY_PHYSICS_3D
  23. /************************************************************************************************************************/
  24. [SerializeField] private Rigidbody _Rigidbody;
  25. [SerializeField] private Vector3 _HitVelocity = new(0, 10, 10);
  26. [SerializeField, Meters] private float _ReturnHeight = -10;
  27. private Vector3 _StartPosition;
  28. /************************************************************************************************************************/
  29. public bool ReadyToHit
  30. => _Rigidbody.isKinematic;
  31. /************************************************************************************************************************/
  32. protected virtual void Awake()
  33. {
  34. _Rigidbody.isKinematic = true;
  35. _StartPosition = _Rigidbody.position;
  36. }
  37. /************************************************************************************************************************/
  38. protected virtual void FixedUpdate()
  39. {
  40. if (_Rigidbody.position.y < _ReturnHeight)
  41. {
  42. _Rigidbody.isKinematic = true;
  43. _Rigidbody.position = _StartPosition;
  44. }
  45. }
  46. /************************************************************************************************************************/
  47. public void Hit()
  48. {
  49. _Rigidbody.isKinematic = false;
  50. #if UNITY_6000_0_OR_NEWER
  51. _Rigidbody.linearVelocity = _HitVelocity;
  52. #else
  53. _Rigidbody.velocity = _HitVelocity;
  54. #endif
  55. }
  56. /************************************************************************************************************************/
  57. #else
  58. /************************************************************************************************************************/
  59. protected virtual void Awake()
  60. {
  61. SampleReadMe.LogMissingPhysics3DModuleError(this);
  62. }
  63. public bool ReadyToHit
  64. => false;
  65. public void Hit() { }
  66. /************************************************************************************************************************/
  67. #endif
  68. /************************************************************************************************************************/
  69. }
  70. }