-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHSpeedShow.cs
70 lines (65 loc) · 2.59 KB
/
HSpeedShow.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using static Unity.IO.LowLevel.Unsafe.AsyncReadManagerMetrics;
public class HSpeedShow : MonoBehaviour
{
public GameObject textParent; //竖向的数字父物体
public GameObject textPrefab;
public GameObject drone;
public GameObject mainText; //主显示数字
public float lineScale = 27f;
public int meterRange = 40;
public AudioSource windFx;
private Vector3 lastPosition;
private float realSpeed;
public static float flatSpd;
private void Start()
{
lastPosition = drone.transform.position;
}
// Update is called once per frame
void Update()
{
mainText.GetComponent<Text>().text = "\u25C1" + flatSpd.ToString("F0") + "km/h";
GenText(MyFuntions.GenMeter(flatSpd, meterRange, textParent.transform,lowLimitStr:"0"));
foreach (Transform tr in textParent.transform) //移动对象到正确的位置
{
var number = int.Parse(tr.name[3..]);
float deltaY = (number - flatSpd) * lineScale;
var mainTextPos = mainText.transform.position;
tr.position = new Vector3(mainTextPos.x-300, (mainTextPos.y + deltaY), mainTextPos.z);
}
}
private void FixedUpdate()
{
var lastFlatPositon = new Vector2(lastPosition.x, lastPosition.z);
var position = drone.transform.position;
var droneFlatPosition = new Vector2(position.x, position.z);
var flatDis = Vector2.Distance(lastFlatPositon, droneFlatPosition);
realSpeed = drone.GetComponent<Rigidbody>().velocity.magnitude;
flatSpd = (flatDis / Time.deltaTime) * 3.6f;
lastPosition = position;
float windVol = Mathf.Clamp(realSpeed*0.01f, 0, 1);
windFx.volume = windVol;
if (windVol == 0 && windFx.isPlaying) windFx.Stop();
if (windVol > 0 && !windFx.isPlaying) windFx.Play();
if (FlyControl.onReplay) windFx.Stop();
}
void GenText(List<int> list)
{
string str;
string showStr;
foreach (int i in list)
{
var newText = Instantiate(textPrefab, new Vector3(0, 0, 0), Quaternion.identity, textParent.transform);
showStr = i.ToString();
//if (i < 10) showStr = "00" + showStr;
//if (i > 10 && i < 100) showStr = "0" + showStr;
if (i % 5 == 0) str = showStr + "-"; else str = "-";
newText.GetComponent<Text>().text = str;
newText.name = "spd" + i.ToString();
}
}
}