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

質問編集履歴

1

1番目のスクリプトSwichを修正し、2番目のスクリプトSwichNewとしました。

2021/02/06 13:00

投稿

maskenzy
maskenzy

スコア0

title CHANGED
File without changes
body CHANGED
@@ -14,10 +14,10 @@
14
14
  複数表示された場合に、進入角度が一番大きいもののみを選択して表示させる機能も想定しています。
15
15
 
16
16
 
17
- スクリプトに関するせつめいは以下の通りです。
17
+ スクリプトに関する説明は以下の通りです。
18
18
  1.pedestrianは歩行者のオブジェクト(複数ある)で、各オブジェクトで距離と進入角度を計算させています。
19
19
 
20
- 2.Qestiontは 距離や進入角度を計算するスクリプトで、各pedestrianについています。距離が閾値以下になると警告灯を起動させることを意図しています。
20
+ 2.swichは 距離や進入角度を計算するスクリプトで、各pedestrianについています。距離が閾値以下になると警告灯を起動させることを意図しています。
21
21
 
22
22
  3.Sphereは光らせたい素子のオブジェクトで、車椅子の子オブジェクトです。まずは 距離が閾値以下の歩行者をすべて提示する設定を考えているので、複数あります。
23
23
 
@@ -25,8 +25,9 @@
25
25
 
26
26
  5. SphereRotateも人にいただいたスクリプトで、 Sphereの色や回転を させるためのスクリプトであると認知しています。各Sphereについています。
27
27
 
28
- Question ではpedestrianの区別をできるようにInspectorからIindexを変えられるようにしたつもりです。実行はできますがどんな閾値に設定してもSphereが光りません。
28
+ swichではpedestrianの区別をできるようにInspectorからIindexを変えられるようにしたつもりです。実行はできますがどんな閾値に設定してもSphereが光りません。
29
29
 
30
+ 6.swichiでほかのオブジェクトの関数の呼び出し方を修正したところうまくいきました。
30
31
 
31
32
  ### 発生している問題・エラーメッセージ
32
33
  まずは一人の歩行者とすれ違うように歩行者の動きを設定しましたが、一人の時ですら警告灯が表示されません。実行はできています。
@@ -41,7 +42,7 @@
41
42
  using System.Collections.Generic;
42
43
  using UnityEngine;
43
44
 
44
- public class Question : MonoBehaviour
45
+ public class swich : MonoBehaviour
45
46
  {
46
47
 
47
48
  GameObject pm;
@@ -105,8 +106,8 @@
105
106
  {
106
107
  CrossingPoint.x = (f * s_p - g * s_w) / e;
107
108
  CrossingPoint.z = (f * u_p - g * u_w) / e;
108
- //ベクトルの計算で求まる衝突想定点の座標
109
+ //ベクトルの計算で求まる衝突想定点の座標
109
-
110
+
110
111
 
111
112
  if (d_r < thresholdOfDistance) //距離がこのスクリプトで決めた閾値以下となれば
112
113
  {
@@ -121,206 +122,94 @@
121
122
 
122
123
  }
123
124
  ```
124
-
125
125
  ```C#
126
126
  using System.Collections;
127
127
  using System.Collections.Generic;
128
128
  using UnityEngine;
129
129
 
130
- public class ControlSphere : MonoBehaviour
130
+ public class swichNew : MonoBehaviour
131
131
  {
132
132
 
133
+ GameObject pm;
134
+ Vector3 lastPos_w;
135
+ Vector3 lastPos_p;
136
+ Vector3 vel_w;
137
+ Vector3 vel_p;
138
+ [Header("値を入力")]
139
+ [Space(5), Tooltip("何人目の歩行者の値であるか")] public int pedIndex;
140
+ [Space(5), Tooltip("警告灯を起動する閾値")] public float thresholdOfDistance;
141
+ [Header("値を観測")]
142
+ [Space(5), Tooltip("電動車椅子との距離")] public float d_r;
143
+ [Space(5), Tooltip("電動車椅子との角度")] public float RelativeAngle;
144
+ [Space(5), Tooltip("すれ違い発生点")] public Vector3 CrossingPoint;
133
145
 
134
- public class PedestrianLightState
146
+ GameObject refObj;//修正箇所
135
- {
136
- public bool isVisible;
137
- public float direction;
147
+ SphereRotate startScript;
138
- public Color color;
139
- public bool isAssigned;
140
- public float priority;
141
148
 
142
- public PedestrianLightState()
143
- {
144
- isVisible = false;
145
- direction = 0;
146
- color = Color.black;
147
- isAssigned = false;
148
- }
149
- }
150
-
151
- [System.Serializable]
152
- public class SphereLightState
153
- {
154
- public SphereRotate sphere;
155
- public int pedIndex;
156
-
157
- public SphereLightState()
158
- {
159
- sphere = null;
160
- pedIndex = -1;
161
- }
162
- }
163
-
164
- public int numPedestrian;
165
- public List<PedestrianLightState> pedestrianLightState = new List<PedestrianLightState>();
166
- public List<SphereLightState> spheres;
167
-
168
149
  // Start is called before the first frame update
169
150
  void Start()
170
151
  {
171
- for (int i = 0; i < numPedestrian; i++)
152
+ pm = GameObject.Find("Wheelchair_High");//車椅子
172
- {
173
- pedestrianLightState.Add(new PedestrianLightState());
153
+ refObj = GameObject.Find("Sphere");//修正箇所
174
- }
175
-
176
- for (int n = 0; n < spheres.Count; n++)
154
+ startScript = refObj.GetComponent<SphereRotate>();//修正箇所
177
- {
178
- spheres[n].sphere.SetInvisible();
179
- }
180
155
  }
181
156
 
182
- // Update is called once per frame
183
157
  void Update()
184
158
  {
185
- //新たにSphereに登録
159
+ float x_w = pm.transform.position.x;
160
+ float z_w = pm.transform.position.z;
186
- for (int i = 0; i < numPedestrian; i++)
161
+ float x_p = this.transform.position.x;
187
- {
188
- if (pedestrianLightState[i].isVisible == true && pedestrianLightState[i].isAssigned == false)
162
+ float z_p = this.transform.position.z;
189
- {
190
163
 
191
- if (spheres.Count != 1)
192
- {
193
- for (int n = 0; n < spheres.Count; n++)
194
- {
195
- if (spheres[n].pedIndex == -1)
196
- {
197
- spheres[n].pedIndex = i;
198
- spheres[n].sphere.SetVisible();
199
- pedestrianLightState[i].isAssigned = true;
200
- //spheres[n].sphere.SetColor(pedestrianLightState[i].color);
201
- //spheres[n].sphere.Rotate(pedestrianLightState[i].direction);
164
+ vel_w = ((pm.transform.position - lastPos_w) / Time.deltaTime);
202
- break;
203
- }
204
- }
205
- }
206
- else
207
- {
208
- if (spheres[0].pedIndex != -1)
209
- {
210
- if (pedestrianLightState[spheres[0].pedIndex].priority < pedestrianLightState[i].priority)
211
- {
212
- pedestrianLightState[spheres[0].pedIndex].isAssigned = false;
213
- spheres[0].pedIndex = i;
214
- pedestrianLightState[i].isAssigned = true;
215
- spheres[0].sphere.SetVisible();
165
+ lastPos_w = pm.transform.position;
216
- }
217
- }
218
- else
219
- {
220
- spheres[0].pedIndex = i;
221
- pedestrianLightState[i].isAssigned = true;
166
+ //(フレーム間の移動距離)÷(フレーム間の時間)で車椅子の速度求めた
222
- spheres[0].sphere.SetVisible();
223
- }
224
167
 
168
+ vel_p = ((this.transform.position - lastPos_p) / Time.deltaTime);
225
- }
169
+ lastPos_p = this.transform.position;
170
+ //(フレーム間の移動距離)÷(フレーム間の時間)で歩行者の速度求めた
226
171
 
227
- }
228
- }
229
- //起動したSphereの状態を変更
230
- for (int n = 0; n < spheres.Count; n++)
172
+ float s_w = vel_w.x;
231
- {
173
+ float u_w = vel_w.z;
232
- if (spheres[n].pedIndex != -1)
174
+ float s_p = vel_p.x;
233
- {
234
- //spheres[n].sphere.SetVisible();
175
+ float u_p = vel_p.z;
235
- spheres[n].sphere.SetColor(pedestrianLightState[spheres[n].pedIndex].color);
236
- spheres[n].sphere.Rotate(pedestrianLightState[spheres[n].pedIndex].direction);
237
- }
238
- }
239
176
 
240
- //消失したSphereを消す
177
+ float a = x_w - x_p;
178
+ float b = z_w - z_p;
179
+ float c = s_w - s_p;
180
+ float d = u_w - u_p;
241
- for (int i = 0; i < numPedestrian; i++)
181
+ float e = s_p * u_w - s_w * u_p;
242
- {
182
+ float f = x_w * u_w - z_w * s_w;
243
- if (pedestrianLightState[i].isVisible == false && pedestrianLightState[i].isAssigned == true)
183
+ float g = x_p * u_p - z_p * s_p;
244
- {
245
184
 
246
- for (int n = 0; n < spheres.Count; n++)
247
- {
248
- if (spheres[n].pedIndex == i)
249
- {
250
- spheres[n].pedIndex = -1;
251
- spheres[n].sphere.SetInvisible();
252
- pedestrianLightState[i].isAssigned = false;
253
- break;
254
- }
255
- }
256
185
 
186
+ Vector3 pos_rel = lastPos_w - lastPos_p;
187
+ RelativeAngle = Vector3.Angle(vel_w, vel_p);
257
- }
188
+ d_r = pos_rel.magnitude;
258
- }
259
- }
260
- }
261
189
 
262
- ```
263
190
 
264
- ```C#
265
- using System.Collections;
191
+ if (t_w > 0 && t_p > 0) //すれ違い前
192
+ {
266
- using System.Collections.Generic;
193
+ CrossingPoint.x = (f * s_p - g * s_w) / e;
267
- using UnityEngine;
194
+ CrossingPoint.z = (f * u_p - g * u_w) / e;
195
+ //ベクトルの計算で求まる衝突想定点の座標
196
+ }
268
197
 
269
- public class SphereRotate : MonoBehaviour
270
- {
271
- // Start is called before the first frame update
198
+ //ここから先で警告灯起動
272
- void Start()
273
- {
274
- vec.y = gameObject.transform.localPosition.y;
275
- rend = gameObject.GetComponent<Renderer>();
276
- rend.material.EnableKeyword("_EMISSION");
277
- flare = gameObject.GetComponent<LensFlare>();
278
- }
279
- float s = 0;
280
- public float rotate, distance = 0.5f;
281
- Vector3 vec;
282
- Renderer rend;
283
199
 
284
- float timer;
285
- bool isSwitch;
286
- Color color1 = new Color(255, 0, 0), color2 = new Color(0, 255, 0), color3 = new Color(0, 0, 255);
200
+ if (d_r < thresholdOfDistance) //距離がこのスクリプトで決めた閾値以下となれば
201
+ {
287
- LensFlare flare;
202
+           //修正箇所
203
+ startScript.pedestrianLightState[pedIndex].isVisible = true;
204
+ startScript.pedestrianLightState[pedIndex].color = Color.red;
288
205
 
289
- // Update is called once per frame
290
- void Update()
291
- {
206
+ }
292
-
293
-
294
207
  }
295
208
 
296
- public void Rotate(float rad)
297
- {
298
- vec.x = distance * Mathf.Cos(rad);
299
- vec.z = distance * Mathf.Sin(rad);
300
- rotate = rad;
301
- gameObject.transform.localPosition = vec;
302
- }
303
209
 
304
- public void SetColor(Color color)
305
- {
306
- rend.material.SetColor("_EmissionColor", color);
307
- flare.color = color;
308
- }
309
-
310
- public void SetInvisible()
311
- {
312
- rend.enabled = false;
313
- flare.enabled = false;
314
- }
315
-
316
- public void SetVisible()
317
- {
318
- rend.enabled = true;
319
- flare.enabled = true;
320
- }
321
210
  }
211
+ ```
322
212
 
323
- ```
324
213
  ### 試したこと
325
214
  閾値の条件を緩くしても表示されませんでした。
326
215
  配列を使うことに慣れていないので、配列の周辺が怪しいです。