ClickToHit.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.Jobs
  5. {
  6. /// <summary>
  7. /// An sample component that calls <see cref="HitReceiver.Hit"/>
  8. /// when the user clicks on the ground.
  9. /// </summary>
  10. ///
  11. /// <remarks>
  12. /// <strong>Sample:</strong>
  13. /// <see href="https://kybernetik.com.au/animancer/docs/samples/jobs/hit-impacts">
  14. /// Hit Impacts</see>
  15. /// </remarks>
  16. ///
  17. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.Jobs/ClickToHit
  18. ///
  19. [AddComponentMenu(Strings.SamplesMenuPrefix + "Jobs - Click to Hit")]
  20. [AnimancerHelpUrl(typeof(ClickToHit))]
  21. public class ClickToHit : MonoBehaviour
  22. {
  23. /************************************************************************************************************************/
  24. #if UNITY_PHYSICS_3D
  25. /************************************************************************************************************************/
  26. [SerializeField] private HitReceiver _HitReceiver;
  27. [SerializeField] private float _LeftClickForce = 500;
  28. [SerializeField] private float _RightClickForce = 300;
  29. /************************************************************************************************************************/
  30. protected virtual void Update()
  31. {
  32. if (SampleInput.LeftMouseDown)
  33. HitTarget(_LeftClickForce);
  34. else if (SampleInput.RightMouseDown)
  35. HitTarget(_RightClickForce);
  36. }
  37. /************************************************************************************************************************/
  38. private void HitTarget(float force)
  39. {
  40. // Get a ray from the main camera in the direction of the mouse cursor.
  41. Ray ray = Camera.main.ScreenPointToRay(SampleInput.MousePosition);
  42. // Raycast with it and stop trying to move it it doesn't hit anything.
  43. if (!Physics.Raycast(ray, out RaycastHit raycastHit))// Note the exclamation mark !
  44. return;
  45. Vector3 direction = _HitReceiver.transform.position - raycastHit.point;
  46. _HitReceiver.Hit(direction, force);
  47. }
  48. /************************************************************************************************************************/
  49. #else
  50. /************************************************************************************************************************/
  51. protected virtual void Awake()
  52. {
  53. SampleReadMe.LogMissingPhysics3DModuleError(this);
  54. }
  55. /************************************************************************************************************************/
  56. #endif
  57. /************************************************************************************************************************/
  58. }
  59. }