MouseDrag.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. using UnityEngine;
  3. namespace Animancer.Samples.InverseKinematics
  4. {
  5. /// <summary>Allows the user to drag any object with a collider around on screen with the mouse.</summary>
  6. ///
  7. /// <remarks>
  8. /// <strong>Sample:</strong>
  9. /// <see href="https://kybernetik.com.au/animancer/docs/samples/ik/puppet">
  10. /// Puppet</see>
  11. /// </remarks>
  12. ///
  13. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.InverseKinematics/MouseDrag
  14. ///
  15. [AddComponentMenu(Strings.SamplesMenuPrefix + "Inverse Kinematics - Mouse Drag")]
  16. [AnimancerHelpUrl(typeof(MouseDrag))]
  17. public class MouseDrag : MonoBehaviour
  18. {
  19. /************************************************************************************************************************/
  20. #if UNITY_PHYSICS_3D
  21. /************************************************************************************************************************/
  22. private Transform _Dragging;
  23. private float _Distance;
  24. /************************************************************************************************************************/
  25. protected virtual void Update()
  26. {
  27. // On click, do a raycast from the mouse, grab whatever it hits, and calculate how far away it is.
  28. if (SampleInput.LeftMouseDown)
  29. {
  30. Ray ray = Camera.main.ScreenPointToRay(SampleInput.MousePosition);
  31. if (Physics.Raycast(ray, out RaycastHit hit) && hit.rigidbody != null)
  32. {
  33. _Dragging = hit.transform;
  34. Transform cameraTransform = Camera.main.transform;
  35. _Distance = Vector3.Dot(_Dragging.position - cameraTransform.position, cameraTransform.forward);
  36. }
  37. return;
  38. }
  39. // While holding the button, move the object in line with the mouse ray.
  40. else if (_Dragging != null && SampleInput.LeftMouseHold)
  41. {
  42. Ray ray = Camera.main.ScreenPointToRay(SampleInput.MousePosition);
  43. Transform cameraTransform = Camera.main.transform;
  44. Vector3 forward = cameraTransform.forward;
  45. float dot = Vector3.Dot(ray.direction, forward);
  46. if (dot > 0)
  47. {
  48. Vector3 planeCenter = cameraTransform.position + forward * _Distance;
  49. Vector3 intersection = ray.origin + ray.direction * Vector3.Dot(planeCenter - ray.origin, forward) / dot;
  50. _Dragging.position = intersection;
  51. return;
  52. }
  53. }
  54. _Dragging = null;
  55. }
  56. /************************************************************************************************************************/
  57. #else
  58. /************************************************************************************************************************/
  59. protected virtual void Awake()
  60. {
  61. SampleReadMe.LogMissingPhysics3DModuleError(this);
  62. }
  63. /************************************************************************************************************************/
  64. #endif
  65. /************************************************************************************************************************/
  66. }
  67. }