RotateLogic.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  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. /// <summary>
  18. /// Update the rotation based on input.
  19. /// </summary>
  20. /// <param name="handsPressedArray">Array with positions of down pointers, order should be the same as handsPressedArray provided in Setup</param>
  21. /// <returns>Desired rotation</returns>
  22. public Quaternion Update(Transform[] handsPressedArray, Quaternion currentRotation)
  23. {
  24. var handlebarDirection = GetHandlebarDirection(handsPressedArray);
  25. return Quaternion.FromToRotation(startHandlebar, handlebarDirection) * startRotation;
  26. }
  27. private static Vector3 GetHandlebarDirection(Transform[] handsPressedArray)
  28. {
  29. Debug.Assert(handsPressedArray.Length > 1);
  30. return handsPressedArray[1].position - handsPressedArray[0].position;
  31. }
  32. }