-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVSpeedShow.cs
83 lines (76 loc) · 2.86 KB
/
VSpeedShow.cs
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class VSpeedShow : MonoBehaviour
{
public GameObject textParent; //竖向的数字父物体
public GameObject textPrefab;
public GameObject drone;
public Text mainText; //主显示数字
public Text altText;
public Text relativeAltText; //相对高度
public float lineScale = 20f;
public int meterRange = 40;
public Image postiveLine, negativeLine;
public static float relativeAlt;
private float height,lastHeight,verSpd;
private int lowLimit, highLimit;
private void Start()
{
lastHeight = drone.transform.position.y;
lowLimit = -meterRange / 2;
highLimit = meterRange / 2;
}
// Update is called once per frame
void Update()
{
float alt = drone.transform.position.y;
altText.text = alt.ToString("F0") + "m";
mainText.text = verSpd.ToString("F1") + "m/s" + "\u25B7";
var calVerSpd = Mathf.Clamp(verSpd,lowLimit, highLimit);
GenText(MyFuntions.GenMeter(calVerSpd, meterRange, textParent.transform,lowLimitStr:lowLimit.ToString(),highLimitStr:highLimit.ToString()));
foreach (Transform tr in textParent.transform) //移动对象到正确的位置
{
var number = int.Parse(tr.name[3..]);
float deltaY = (number - calVerSpd) * lineScale;
var mainTextPos = mainText.transform.position;
tr.position = new Vector3(mainTextPos.x + 300, (mainTextPos.y + deltaY), mainTextPos.z); //80是主文字的x宽度的一半
}
float vsPercent;
if (calVerSpd < 0)
{
vsPercent = Mathf.Clamp(-calVerSpd / -lowLimit, 0, 1);
negativeLine.fillAmount = vsPercent;
postiveLine.fillAmount = 0;
}
else
{
vsPercent = Mathf.Clamp(calVerSpd / highLimit, 0, 1);
negativeLine.fillAmount = 0;
postiveLine.fillAmount = vsPercent;
}
var th = Terrain.activeTerrain.SampleHeight(drone.transform.position);
relativeAlt = alt - th;
relativeAltText.text = "H:" + relativeAlt.ToString("F1") + "m";
}
private void FixedUpdate()
{
height = drone.transform.position.y;
var vetDis = height - lastHeight;
verSpd = vetDis / (Time.deltaTime);
lastHeight = height;
}
void GenText(List<int> list)
{
string str;
foreach (int i in list)
{
var newText = Instantiate(textPrefab, new Vector3(0, 0, 0), Quaternion.identity, textParent.transform);
if (i % 5 == 0) str = "-" + i.ToString(); else str = "-";
//if (i == 0) str = "-0";
newText.GetComponent<Text>().text = str;
newText.name = "vsp" + i.ToString();
}
}
}