ClickToInteract.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. using UnityEngine;
  3. namespace Animancer.Samples.FineControl
  4. {
  5. /// <summary>
  6. /// Attempts to interact with whatever <see cref="IInteractable"/>
  7. /// the cursor is pointing at when the user clicks the mouse.
  8. /// </summary>
  9. ///
  10. /// <remarks>
  11. /// <strong>Sample:</strong>
  12. /// <see href="https://kybernetik.com.au/animancer/docs/samples/fine-control/doors">
  13. /// Doors</see>
  14. /// </remarks>
  15. ///
  16. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.FineControl/ClickToInteract
  17. ///
  18. [AddComponentMenu(Strings.SamplesMenuPrefix + "Fine Control - Click To Interact")]
  19. [AnimancerHelpUrl(typeof(ClickToInteract))]
  20. public class ClickToInteract : MonoBehaviour
  21. {
  22. /************************************************************************************************************************/
  23. #if UNITY_PHYSICS_3D
  24. /************************************************************************************************************************/
  25. protected virtual void Update()
  26. {
  27. if (!SampleInput.LeftMouseUp)
  28. return;
  29. Ray ray = Camera.main.ScreenPointToRay(SampleInput.MousePosition);
  30. if (Physics.Raycast(ray, out RaycastHit raycastHit))
  31. {
  32. IInteractable interactable = raycastHit.collider.GetComponentInParent<IInteractable>();
  33. interactable?.Interact();
  34. }
  35. }
  36. /************************************************************************************************************************/
  37. #else
  38. /************************************************************************************************************************/
  39. protected virtual void Awake()
  40. {
  41. SampleReadMe.LogMissingPhysics3DModuleError(this);
  42. }
  43. /************************************************************************************************************************/
  44. #endif
  45. /************************************************************************************************************************/
  46. }
  47. }