回答編集履歴

1

コードの追加

2020/01/06 16:07

投稿

mitatoshi
mitatoshi

スコア12

test CHANGED
@@ -1,19 +1,253 @@
1
+ ### 変更点
2
+
3
+ 閾値としていたthresholdはpeakとなっております。また、もともとのコードより追加された点が多数ありますので、一回だけ入力されたデータを処理して終えたい場合はprivate bool locked = false;などを参照してください
4
+
5
+ ```ここに言語を入力
6
+
7
+ using System.Collections;
8
+
1
- private bool locked =false;
9
+ using System.Collections.Generic;
10
+
2
-
11
+ using UnityEngine;
12
+
13
+ using UnityEngine.UI;
14
+
15
+
16
+
3
- void OnDataReceived(string message)
17
+ public class SerialCube : MonoBehaviour
18
+
19
+
4
20
 
5
21
  {
6
22
 
23
+
24
+
25
+ public SerialHandler serialHandler;
26
+
27
+
28
+
29
+ public float peak = 0f;
30
+
31
+
32
+
33
+ public Vector3 velocity = new Vector3(0f, 0f,2.0f);
34
+
35
+ public Text sensorText;
36
+
37
+ public Text scoreText;
38
+
39
+ public Text startText;
40
+
41
+ public Text finishText;
42
+
43
+ private bool levelDown = false;
44
+
45
+ private int score;
46
+
47
+ private int start;
48
+
49
+ public static float Score { get; internal set; }
50
+
51
+ public static float starT { get; internal set; }
52
+
7
-  if(start ==1 &&!locked)
53
+ private bool locked = false;
54
+
55
+
56
+
57
+
58
+
59
+
60
+
8
-
61
+ // private bool levelUp = false;
62
+
63
+
64
+
65
+
66
+
67
+
68
+
69
+ void Start()
70
+
71
+
72
+
9
- {
73
+ {
74
+
75
+
76
+
10
-
77
+ serialHandler.OnDataReceived += OnDataReceived;
78
+
79
+ score = 0;
80
+
81
+ SetCountText();
82
+
83
+ start = 0;
84
+
85
+ setcountText();
86
+
87
+
88
+
89
+ }
90
+
91
+ private void Update()
92
+
93
+ {
94
+
95
+ if (Goal.goal == true)
96
+
97
+ {
98
+
99
+ finishText.text = "Goal";
100
+
101
+ }
102
+
103
+ }
104
+
105
+
106
+
107
+ void OnDataReceived(string message)
108
+
109
+
110
+
111
+ {
112
+
113
+
114
+
115
+ var data = message.Split(
116
+
117
+
118
+
119
+ new string[] { "\t" },
120
+
121
+
122
+
123
+ System.StringSplitOptions.None);
124
+
125
+
126
+
127
+ if (data.Length < 2) return;
128
+
129
+ sensorText.text = "ondo:" + message;
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+ try
138
+
139
+
140
+
141
+ {
142
+
143
+ var temperature = float.Parse(data[0]);
144
+
145
+
146
+
147
+ if (start == 1 && !locked)
148
+
149
+ {
150
+
151
+ locked = true; // 二度とif文に入らないようにする
152
+
153
+ peak = temperature; // 閾値を現在の温度にする
154
+
155
+ peak += 30;
156
+
157
+ }
158
+
159
+
160
+
161
+
162
+
163
+ if (temperature > peak)
164
+
165
+ {
166
+
167
+ transform.localPosition += (levelDown) ? new Vector3(0.0f, 240.0f, 2.0f) * Time.deltaTime : velocity * Time.deltaTime;
168
+
169
+ levelDown = false;
170
+
171
+ }
172
+
173
+ else
174
+
175
+ {
176
+
177
+ transform.localPosition += velocity * Time.deltaTime;
178
+
11
-  locked =true;
179
+ levelDown = true;
180
+
12
-
181
+ }
182
+
183
+ }
184
+
185
+
186
+
187
+
188
+
13
-  threshold = temperature
189
+ catch (System.Exception e)
190
+
191
+
192
+
193
+ {
194
+
195
+
196
+
197
+ Debug.LogWarning(e.Message);
198
+
199
+
200
+
201
+ }
202
+
203
+
204
+
205
+ }
206
+
207
+
208
+
209
+ private void OnTriggerEnter(Collider other)
210
+
211
+ {
212
+
213
+ if (other.gameObject.CompareTag("Area"))
214
+
215
+ {
216
+
217
+ score += 1;
218
+
219
+ SetCountText();
220
+
221
+ }
222
+
223
+ if (other.gameObject.CompareTag("start"))
224
+
225
+ {
226
+
227
+ start += 1;
228
+
229
+ setcountText();
230
+
231
+ }
232
+
233
+ }
234
+
235
+ void SetCountText()
236
+
237
+ {
238
+
239
+ scoreText.text = "Score : " + score.ToString();
240
+
241
+ }
242
+
243
+ void setcountText()
244
+
245
+ {
246
+
247
+ startText.text = "start : " + start.ToString();
248
+
249
+ }
14
250
 
15
251
  }
16
252
 
17
- }
253
+ ```
18
-
19
- を付け加えて解決しました