質問編集履歴
1
1番目のスクリプトSwichを修正し、2番目のスクリプトSwichNewとしました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -30,13 +30,13 @@
|
|
30
30
|
|
31
31
|
|
32
32
|
|
33
|
-
スクリプトに関する
|
33
|
+
スクリプトに関する説明は以下の通りです。
|
34
34
|
|
35
35
|
1.pedestrianは歩行者のオブジェクト(複数ある)で、各オブジェクトで距離と進入角度を計算させています。
|
36
36
|
|
37
37
|
|
38
38
|
|
39
|
-
2.
|
39
|
+
2.swichは 距離や進入角度を計算するスクリプトで、各pedestrianについています。距離が閾値以下になると警告灯を起動させることを意図しています。
|
40
40
|
|
41
41
|
|
42
42
|
|
@@ -52,9 +52,11 @@
|
|
52
52
|
|
53
53
|
|
54
54
|
|
55
|
-
|
55
|
+
swichではpedestrianの区別をできるようにInspectorからIindexを変えられるようにしたつもりです。実行はできますがどんな閾値に設定してもSphereが光りません。
|
56
|
+
|
57
|
+
|
58
|
+
|
56
|
-
|
59
|
+
6.swichiでほかのオブジェクトの関数の呼び出し方を修正したところうまくいきました。
|
57
|
-
|
58
60
|
|
59
61
|
|
60
62
|
|
@@ -84,7 +86,7 @@
|
|
84
86
|
|
85
87
|
|
86
88
|
|
87
|
-
public class
|
89
|
+
public class swich : MonoBehaviour
|
88
90
|
|
89
91
|
{
|
90
92
|
|
@@ -212,9 +214,9 @@
|
|
212
214
|
|
213
215
|
CrossingPoint.z = (f * u_p - g * u_w) / e;
|
214
216
|
|
215
|
-
//ベクトルの計算で求まる衝突想定点の座標
|
217
|
+
//ベクトルの計算で求まる衝突想定点の座標
|
216
|
-
|
217
|
-
|
218
|
+
|
219
|
+
|
218
220
|
|
219
221
|
|
220
222
|
|
@@ -244,8 +246,6 @@
|
|
244
246
|
|
245
247
|
```
|
246
248
|
|
247
|
-
|
248
|
-
|
249
249
|
```C#
|
250
250
|
|
251
251
|
using System.Collections;
|
@@ -256,394 +256,172 @@
|
|
256
256
|
|
257
257
|
|
258
258
|
|
259
|
-
public class
|
259
|
+
public class swichNew : MonoBehaviour
|
260
260
|
|
261
261
|
{
|
262
262
|
|
263
263
|
|
264
264
|
|
265
|
-
|
265
|
+
GameObject pm;
|
266
|
+
|
266
|
-
|
267
|
+
Vector3 lastPos_w;
|
268
|
+
|
269
|
+
Vector3 lastPos_p;
|
270
|
+
|
271
|
+
Vector3 vel_w;
|
272
|
+
|
273
|
+
Vector3 vel_p;
|
274
|
+
|
275
|
+
[Header("値を入力")]
|
276
|
+
|
277
|
+
[Space(5), Tooltip("何人目の歩行者の値であるか")] public int pedIndex;
|
278
|
+
|
279
|
+
[Space(5), Tooltip("警告灯を起動する閾値")] public float thresholdOfDistance;
|
280
|
+
|
281
|
+
[Header("値を観測")]
|
282
|
+
|
283
|
+
[Space(5), Tooltip("電動車椅子との距離")] public float d_r;
|
284
|
+
|
285
|
+
[Space(5), Tooltip("電動車椅子との角度")] public float RelativeAngle;
|
286
|
+
|
287
|
+
[Space(5), Tooltip("すれ違い発生点")] public Vector3 CrossingPoint;
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
GameObject refObj;//修正箇所
|
292
|
+
|
267
|
-
p
|
293
|
+
SphereRotate startScript;
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
// Start is called before the first frame update
|
298
|
+
|
299
|
+
void Start()
|
268
300
|
|
269
301
|
{
|
270
302
|
|
303
|
+
pm = GameObject.Find("Wheelchair_High");//車椅子
|
304
|
+
|
305
|
+
refObj = GameObject.Find("Sphere");//修正箇所
|
306
|
+
|
307
|
+
startScript = refObj.GetComponent<SphereRotate>();//修正箇所
|
308
|
+
|
309
|
+
}
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
void Update()
|
314
|
+
|
315
|
+
{
|
316
|
+
|
317
|
+
float x_w = pm.transform.position.x;
|
318
|
+
|
319
|
+
float z_w = pm.transform.position.z;
|
320
|
+
|
321
|
+
float x_p = this.transform.position.x;
|
322
|
+
|
323
|
+
float z_p = this.transform.position.z;
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
vel_w = ((pm.transform.position - lastPos_w) / Time.deltaTime);
|
328
|
+
|
329
|
+
lastPos_w = pm.transform.position;
|
330
|
+
|
331
|
+
//(フレーム間の移動距離)÷(フレーム間の時間)で車椅子の速度求めた
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
vel_p = ((this.transform.position - lastPos_p) / Time.deltaTime);
|
336
|
+
|
337
|
+
lastPos_p = this.transform.position;
|
338
|
+
|
339
|
+
//(フレーム間の移動距離)÷(フレーム間の時間)で歩行者の速度求めた
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
float s_w = vel_w.x;
|
344
|
+
|
345
|
+
float u_w = vel_w.z;
|
346
|
+
|
347
|
+
float s_p = vel_p.x;
|
348
|
+
|
349
|
+
float u_p = vel_p.z;
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
float a = x_w - x_p;
|
354
|
+
|
271
|
-
|
355
|
+
float b = z_w - z_p;
|
272
|
-
|
273
|
-
|
356
|
+
|
274
|
-
|
275
|
-
|
357
|
+
float c = s_w - s_p;
|
358
|
+
|
276
|
-
|
359
|
+
float d = u_w - u_p;
|
360
|
+
|
361
|
+
float e = s_p * u_w - s_w * u_p;
|
362
|
+
|
363
|
+
float f = x_w * u_w - z_w * s_w;
|
364
|
+
|
365
|
+
float g = x_p * u_p - z_p * s_p;
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
Vector3 pos_rel = lastPos_w - lastPos_p;
|
372
|
+
|
373
|
+
RelativeAngle = Vector3.Angle(vel_w, vel_p);
|
374
|
+
|
277
|
-
p
|
375
|
+
d_r = pos_rel.magnitude;
|
278
|
-
|
279
|
-
|
376
|
+
|
280
|
-
|
281
|
-
|
282
|
-
|
377
|
+
|
378
|
+
|
379
|
+
|
380
|
+
|
283
|
-
|
381
|
+
if (t_w > 0 && t_p > 0) //すれ違い前
|
284
382
|
|
285
383
|
{
|
286
384
|
|
287
|
-
isVisible = false;
|
288
|
-
|
289
|
-
direction = 0;
|
290
|
-
|
291
|
-
color = Color.black;
|
292
|
-
|
293
|
-
|
385
|
+
CrossingPoint.x = (f * s_p - g * s_w) / e;
|
386
|
+
|
387
|
+
CrossingPoint.z = (f * u_p - g * u_w) / e;
|
388
|
+
|
389
|
+
//ベクトルの計算で求まる衝突想定点の座標
|
294
390
|
|
295
391
|
}
|
296
392
|
|
393
|
+
|
394
|
+
|
395
|
+
//ここから先で警告灯起動
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
if (d_r < thresholdOfDistance) //距離がこのスクリプトで決めた閾値以下となれば
|
400
|
+
|
401
|
+
{
|
402
|
+
|
403
|
+
//修正箇所
|
404
|
+
|
405
|
+
startScript.pedestrianLightState[pedIndex].isVisible = true;
|
406
|
+
|
407
|
+
startScript.pedestrianLightState[pedIndex].color = Color.red;
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
}
|
412
|
+
|
297
413
|
}
|
298
414
|
|
299
415
|
|
300
416
|
|
301
|
-
|
417
|
+
|
302
|
-
|
303
|
-
public class SphereLightState
|
304
|
-
|
305
|
-
{
|
306
|
-
|
307
|
-
public SphereRotate sphere;
|
308
|
-
|
309
|
-
public int pedIndex;
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
public SphereLightState()
|
314
|
-
|
315
|
-
{
|
316
|
-
|
317
|
-
sphere = null;
|
318
|
-
|
319
|
-
pedIndex = -1;
|
320
|
-
|
321
|
-
}
|
322
|
-
|
323
|
-
}
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
public int numPedestrian;
|
328
|
-
|
329
|
-
public List<PedestrianLightState> pedestrianLightState = new List<PedestrianLightState>();
|
330
|
-
|
331
|
-
public List<SphereLightState> spheres;
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
// Start is called before the first frame update
|
336
|
-
|
337
|
-
void Start()
|
338
|
-
|
339
|
-
{
|
340
|
-
|
341
|
-
for (int i = 0; i < numPedestrian; i++)
|
342
|
-
|
343
|
-
{
|
344
|
-
|
345
|
-
pedestrianLightState.Add(new PedestrianLightState());
|
346
|
-
|
347
|
-
}
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
for (int n = 0; n < spheres.Count; n++)
|
352
|
-
|
353
|
-
{
|
354
|
-
|
355
|
-
spheres[n].sphere.SetInvisible();
|
356
|
-
|
357
|
-
}
|
358
|
-
|
359
|
-
}
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
// Update is called once per frame
|
364
|
-
|
365
|
-
void Update()
|
366
|
-
|
367
|
-
{
|
368
|
-
|
369
|
-
//新たにSphereに登録
|
370
|
-
|
371
|
-
for (int i = 0; i < numPedestrian; i++)
|
372
|
-
|
373
|
-
{
|
374
|
-
|
375
|
-
if (pedestrianLightState[i].isVisible == true && pedestrianLightState[i].isAssigned == false)
|
376
|
-
|
377
|
-
{
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
if (spheres.Count != 1)
|
382
|
-
|
383
|
-
{
|
384
|
-
|
385
|
-
for (int n = 0; n < spheres.Count; n++)
|
386
|
-
|
387
|
-
{
|
388
|
-
|
389
|
-
if (spheres[n].pedIndex == -1)
|
390
|
-
|
391
|
-
{
|
392
|
-
|
393
|
-
spheres[n].pedIndex = i;
|
394
|
-
|
395
|
-
spheres[n].sphere.SetVisible();
|
396
|
-
|
397
|
-
pedestrianLightState[i].isAssigned = true;
|
398
|
-
|
399
|
-
//spheres[n].sphere.SetColor(pedestrianLightState[i].color);
|
400
|
-
|
401
|
-
//spheres[n].sphere.Rotate(pedestrianLightState[i].direction);
|
402
|
-
|
403
|
-
break;
|
404
|
-
|
405
|
-
}
|
406
|
-
|
407
|
-
}
|
408
|
-
|
409
|
-
}
|
410
|
-
|
411
|
-
else
|
412
|
-
|
413
|
-
{
|
414
|
-
|
415
|
-
if (spheres[0].pedIndex != -1)
|
416
|
-
|
417
|
-
{
|
418
|
-
|
419
|
-
if (pedestrianLightState[spheres[0].pedIndex].priority < pedestrianLightState[i].priority)
|
420
|
-
|
421
|
-
{
|
422
|
-
|
423
|
-
pedestrianLightState[spheres[0].pedIndex].isAssigned = false;
|
424
|
-
|
425
|
-
spheres[0].pedIndex = i;
|
426
|
-
|
427
|
-
pedestrianLightState[i].isAssigned = true;
|
428
|
-
|
429
|
-
spheres[0].sphere.SetVisible();
|
430
|
-
|
431
|
-
}
|
432
|
-
|
433
|
-
}
|
434
|
-
|
435
|
-
else
|
436
|
-
|
437
|
-
{
|
438
|
-
|
439
|
-
spheres[0].pedIndex = i;
|
440
|
-
|
441
|
-
pedestrianLightState[i].isAssigned = true;
|
442
|
-
|
443
|
-
spheres[0].sphere.SetVisible();
|
444
|
-
|
445
|
-
}
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
}
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
}
|
454
|
-
|
455
|
-
}
|
456
|
-
|
457
|
-
//起動したSphereの状態を変更
|
458
|
-
|
459
|
-
for (int n = 0; n < spheres.Count; n++)
|
460
|
-
|
461
|
-
{
|
462
|
-
|
463
|
-
if (spheres[n].pedIndex != -1)
|
464
|
-
|
465
|
-
{
|
466
|
-
|
467
|
-
//spheres[n].sphere.SetVisible();
|
468
|
-
|
469
|
-
spheres[n].sphere.SetColor(pedestrianLightState[spheres[n].pedIndex].color);
|
470
|
-
|
471
|
-
spheres[n].sphere.Rotate(pedestrianLightState[spheres[n].pedIndex].direction);
|
472
|
-
|
473
|
-
}
|
474
|
-
|
475
|
-
}
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
//消失したSphereを消す
|
480
|
-
|
481
|
-
for (int i = 0; i < numPedestrian; i++)
|
482
|
-
|
483
|
-
{
|
484
|
-
|
485
|
-
if (pedestrianLightState[i].isVisible == false && pedestrianLightState[i].isAssigned == true)
|
486
|
-
|
487
|
-
{
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
for (int n = 0; n < spheres.Count; n++)
|
492
|
-
|
493
|
-
{
|
494
|
-
|
495
|
-
if (spheres[n].pedIndex == i)
|
496
|
-
|
497
|
-
{
|
498
|
-
|
499
|
-
spheres[n].pedIndex = -1;
|
500
|
-
|
501
|
-
spheres[n].sphere.SetInvisible();
|
502
|
-
|
503
|
-
pedestrianLightState[i].isAssigned = false;
|
504
|
-
|
505
|
-
break;
|
506
|
-
|
507
|
-
}
|
508
|
-
|
509
|
-
}
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
}
|
514
|
-
|
515
|
-
}
|
516
|
-
|
517
|
-
}
|
518
418
|
|
519
419
|
}
|
520
420
|
|
521
|
-
|
522
|
-
|
523
421
|
```
|
524
422
|
|
525
423
|
|
526
424
|
|
527
|
-
```C#
|
528
|
-
|
529
|
-
using System.Collections;
|
530
|
-
|
531
|
-
using System.Collections.Generic;
|
532
|
-
|
533
|
-
using UnityEngine;
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
public class SphereRotate : MonoBehaviour
|
538
|
-
|
539
|
-
{
|
540
|
-
|
541
|
-
// Start is called before the first frame update
|
542
|
-
|
543
|
-
void Start()
|
544
|
-
|
545
|
-
{
|
546
|
-
|
547
|
-
vec.y = gameObject.transform.localPosition.y;
|
548
|
-
|
549
|
-
rend = gameObject.GetComponent<Renderer>();
|
550
|
-
|
551
|
-
rend.material.EnableKeyword("_EMISSION");
|
552
|
-
|
553
|
-
flare = gameObject.GetComponent<LensFlare>();
|
554
|
-
|
555
|
-
}
|
556
|
-
|
557
|
-
float s = 0;
|
558
|
-
|
559
|
-
public float rotate, distance = 0.5f;
|
560
|
-
|
561
|
-
Vector3 vec;
|
562
|
-
|
563
|
-
Renderer rend;
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
float timer;
|
568
|
-
|
569
|
-
bool isSwitch;
|
570
|
-
|
571
|
-
Color color1 = new Color(255, 0, 0), color2 = new Color(0, 255, 0), color3 = new Color(0, 0, 255);
|
572
|
-
|
573
|
-
LensFlare flare;
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
// Update is called once per frame
|
578
|
-
|
579
|
-
void Update()
|
580
|
-
|
581
|
-
{
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
}
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
public void Rotate(float rad)
|
592
|
-
|
593
|
-
{
|
594
|
-
|
595
|
-
vec.x = distance * Mathf.Cos(rad);
|
596
|
-
|
597
|
-
vec.z = distance * Mathf.Sin(rad);
|
598
|
-
|
599
|
-
rotate = rad;
|
600
|
-
|
601
|
-
gameObject.transform.localPosition = vec;
|
602
|
-
|
603
|
-
}
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
public void SetColor(Color color)
|
608
|
-
|
609
|
-
{
|
610
|
-
|
611
|
-
rend.material.SetColor("_EmissionColor", color);
|
612
|
-
|
613
|
-
flare.color = color;
|
614
|
-
|
615
|
-
}
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
public void SetInvisible()
|
620
|
-
|
621
|
-
{
|
622
|
-
|
623
|
-
rend.enabled = false;
|
624
|
-
|
625
|
-
flare.enabled = false;
|
626
|
-
|
627
|
-
}
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
public void SetVisible()
|
632
|
-
|
633
|
-
{
|
634
|
-
|
635
|
-
rend.enabled = true;
|
636
|
-
|
637
|
-
flare.enabled = true;
|
638
|
-
|
639
|
-
}
|
640
|
-
|
641
|
-
}
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
```
|
646
|
-
|
647
425
|
### 試したこと
|
648
426
|
|
649
427
|
閾値の条件を緩くしても表示されませんでした。
|