teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

コードの追加

2020/01/06 16:07

投稿

mitatoshi
mitatoshi

スコア12

answer CHANGED
@@ -1,10 +1,127 @@
1
+ ### 変更点
2
+ 閾値としていたthresholdはpeakとなっております。また、もともとのコードより追加された点が多数ありますので、一回だけ入力されたデータを処理して終えたい場合はprivate bool locked = false;などを参照してください
3
+ ```ここに言語を入力
4
+ using System.Collections;
1
- private bool locked =false;
5
+ using System.Collections.Generic;
6
+ using UnityEngine;
7
+ using UnityEngine.UI;
8
+
2
- void OnDataReceived(string message)
9
+ public class SerialCube : MonoBehaviour
10
+
3
11
  {
12
+
13
+ public SerialHandler serialHandler;
14
+
15
+ public float peak = 0f;
16
+
17
+ public Vector3 velocity = new Vector3(0f, 0f,2.0f);
18
+ public Text sensorText;
19
+ public Text scoreText;
20
+ public Text startText;
21
+ public Text finishText;
22
+ private bool levelDown = false;
23
+ private int score;
24
+ private int start;
25
+ public static float Score { get; internal set; }
26
+ public static float starT { get; internal set; }
4
-  if(start ==1 &&!locked)
27
+ private bool locked = false;
28
+
29
+
30
+
31
+ // private bool levelUp = false;
32
+
33
+
34
+
35
+ void Start()
36
+
5
- {
37
+ {
38
+
39
+ serialHandler.OnDataReceived += OnDataReceived;
40
+ score = 0;
41
+ SetCountText();
42
+ start = 0;
43
+ setcountText();
44
+
45
+ }
46
+ private void Update()
47
+ {
48
+ if (Goal.goal == true)
49
+ {
50
+ finishText.text = "Goal";
51
+ }
52
+ }
53
+
54
+ void OnDataReceived(string message)
55
+
56
+ {
57
+
58
+ var data = message.Split(
59
+
60
+ new string[] { "\t" },
61
+
62
+ System.StringSplitOptions.None);
63
+
64
+ if (data.Length < 2) return;
65
+ sensorText.text = "ondo:" + message;
66
+
67
+
68
+
69
+ try
70
+
71
+ {
72
+ var temperature = float.Parse(data[0]);
73
+
74
+ if (start == 1 && !locked)
75
+ {
76
+ locked = true; // 二度とif文に入らないようにする
77
+ peak = temperature; // 閾値を現在の温度にする
78
+ peak += 30;
79
+ }
80
+
81
+
82
+ if (temperature > peak)
83
+ {
84
+ transform.localPosition += (levelDown) ? new Vector3(0.0f, 240.0f, 2.0f) * Time.deltaTime : velocity * Time.deltaTime;
85
+ levelDown = false;
86
+ }
87
+ else
88
+ {
89
+ transform.localPosition += velocity * Time.deltaTime;
6
-  locked =true;
90
+ levelDown = true;
91
+ }
92
+ }
93
+
94
+
7
-  threshold = temperature
95
+ catch (System.Exception e)
96
+
97
+ {
98
+
99
+ Debug.LogWarning(e.Message);
100
+
101
+ }
102
+
103
+ }
104
+
105
+ private void OnTriggerEnter(Collider other)
106
+ {
107
+ if (other.gameObject.CompareTag("Area"))
108
+ {
109
+ score += 1;
110
+ SetCountText();
111
+ }
112
+ if (other.gameObject.CompareTag("start"))
113
+ {
114
+ start += 1;
115
+ setcountText();
116
+ }
117
+ }
118
+ void SetCountText()
119
+ {
120
+ scoreText.text = "Score : " + score.ToString();
121
+ }
122
+ void setcountText()
123
+ {
124
+ startText.text = "start : " + start.ToString();
125
+ }
8
126
  }
9
- }
127
+ ```
10
- を付け加えて解決しました