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