ScaleLogic.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ScaleLogic
  5. {
  6. private Vector3 startObjectScale;
  7. private float startHandDistanceMeters;
  8. /// <summary>
  9. /// Initialize system with source info from controllers/hands
  10. /// </summary>
  11. /// <param name="handsPressedArray">Array with positions of down pointers</param>
  12. /// <param name="manipulationRoot">Transform of gameObject to be manipulated</param>
  13. public virtual void Setup(Transform[] handsPressedArray, Transform manipulationRoot)
  14. {
  15. startHandDistanceMeters = GetMinDistanceBetweenHands(handsPressedArray);
  16. startObjectScale = manipulationRoot.transform.localScale;
  17. }
  18. /// <summary>
  19. /// update GameObject with new Scale state
  20. /// </summary>
  21. /// <param name="handsPressedArray">Array with positions of down pointers, order should be the same as handsPressedArray provided in Setup</param>
  22. /// <returns>a Vector3 describing the new Scale of the object being manipulated</returns>
  23. public virtual Vector3 UpdateMap(Transform[] handsPressedArray)
  24. {
  25. var ratioMultiplier = GetMinDistanceBetweenHands(handsPressedArray) / startHandDistanceMeters;
  26. return startObjectScale * ratioMultiplier;
  27. }
  28. private float GetMinDistanceBetweenHands(Transform[] handsPressedArray)
  29. {
  30. var result = float.MaxValue;
  31. for (int i = 0; i < handsPressedArray.Length; i++)
  32. {
  33. for (int j = i + 1; j < handsPressedArray.Length; j++)
  34. {
  35. var distance = Vector3.Distance(handsPressedArray[i].position, handsPressedArray[j].position);
  36. if (distance < result)
  37. {
  38. result = distance;
  39. }
  40. }
  41. }
  42. return result;
  43. }
  44. }