RotateLogic.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class RotateLogic
  5. {
  6. private Vector3 startHandlebar;
  7. private Quaternion startRotation;
  8. /// <summary>
  9. /// Setup the rotation logic.
  10. /// </summary>
  11. /// <param name="handsPressedArray">Array with positions of down pointers</param>
  12. public void Setup(Transform[] handsPressedArray, Transform t)
  13. {
  14. startHandlebar = GetHandlebarDirection(handsPressedArray);
  15. startRotation = t.rotation;
  16. }
  17. public void Setup(Vector3[] handsPressedArray, Transform t)
  18. {
  19. startHandlebar = GetHandlebarDirection(handsPressedArray);
  20. startRotation = t.rotation;
  21. }
  22. /// <summary>
  23. /// Update the rotation based on input.
  24. /// </summary>
  25. /// <param name="handsPressedArray">Array with positions of down pointers, order should be the same as handsPressedArray provided in Setup</param>
  26. /// <returns>Desired rotation</returns>
  27. public Quaternion Update(Transform[] handsPressedArray, Quaternion currentRotation)
  28. {
  29. var handlebarDirection = GetHandlebarDirection(handsPressedArray);
  30. return Quaternion.FromToRotation(startHandlebar, handlebarDirection) * startRotation;
  31. }
  32. public Quaternion Update(Vector3[] handsPressedArray, Quaternion currentRotation)
  33. {
  34. var handlebarDirection = GetHandlebarDirection(handsPressedArray);
  35. return Quaternion.FromToRotation(startHandlebar, handlebarDirection) * startRotation;
  36. }
  37. private static Vector3 GetHandlebarDirection(Transform[] handsPressedArray)
  38. {
  39. Debug.Assert(handsPressedArray.Length > 1);
  40. return handsPressedArray[1].position - handsPressedArray[0].position;
  41. }
  42. private static Vector3 GetHandlebarDirection(Vector3[] handsPressedArray)
  43. {
  44. Debug.Assert(handsPressedArray.Length > 1);
  45. return handsPressedArray[1] - handsPressedArray[0];
  46. }
  47. }