TargetMeasurement.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. using UnityEngine;
  3. using System.Collections;
  4. using UnityEngine.UI;
  5. namespace Valve.VR.InteractionSystem.Sample
  6. {
  7. public class TargetMeasurement : MonoBehaviour
  8. {
  9. public GameObject visualWrapper;
  10. public Transform measurementTape;
  11. public Transform endPoint;
  12. public Text measurementTextM;
  13. public Text measurementTextFT;
  14. public float maxDistanceToDraw = 6f;
  15. public bool drawTape = false;
  16. private float lastDistance;
  17. private void Update()
  18. {
  19. if (Camera.main != null)
  20. {
  21. Vector3 fromPoint = Camera.main.transform.position;
  22. fromPoint.y = endPoint.position.y;
  23. float distance = Vector3.Distance(fromPoint, endPoint.position);
  24. Vector3 center = Vector3.Lerp(fromPoint, endPoint.position, 0.5f);
  25. this.transform.position = center;
  26. this.transform.forward = endPoint.position - fromPoint;
  27. measurementTape.localScale = new Vector3(0.05f, distance, 0.05f);
  28. if (Mathf.Abs(distance - lastDistance) > 0.01f)
  29. {
  30. measurementTextM.text = distance.ToString("00.0m");
  31. measurementTextFT.text = (distance * 3.28084).ToString("00.0ft");
  32. lastDistance = distance;
  33. }
  34. if (drawTape)
  35. visualWrapper.SetActive(distance < maxDistanceToDraw);
  36. else
  37. visualWrapper.SetActive(false);
  38. }
  39. }
  40. }
  41. }