質問編集履歴

7

2021/04/26 03:27

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -612,18 +612,18 @@
612
612
 
613
613
 
614
614
 
615
+ ここでcosを求めないといけませんでした。
616
+
617
+ ```c#
618
+
619
+
620
+
615
621
  var en_up = transform.up;
616
622
 
617
623
  var en_to_pi = (playerPos.transform.position-transform.position);
618
624
 
619
625
 
620
626
 
621
- ここでcosを求めないといけませんでした。
622
-
623
- ```c#
624
-
625
-
626
-
627
627
  var cos = Vector3.Dot(en_up,en_to_pi)/(en_up.magnitude*en_to_pi.magnitude );
628
628
 
629
629
  var deg = Mathf.Rad2Deg * Mathf.Acos(cos);

6

2021/04/26 03:26

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -620,6 +620,14 @@
620
620
 
621
621
  ここでcosを求めないといけませんでした。
622
622
 
623
+ ```c#
624
+
625
+
626
+
623
627
  var cos = Vector3.Dot(en_up,en_to_pi)/(en_up.magnitude*en_to_pi.magnitude );
624
628
 
625
629
  var deg = Mathf.Rad2Deg * Mathf.Acos(cos);
630
+
631
+
632
+
633
+ ```

5

2021/04/26 03:25

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -454,7 +454,7 @@
454
454
 
455
455
 
456
456
 
457
- ### 追記
457
+ ### 追記1
458
458
 
459
459
 
460
460
 
@@ -603,3 +603,23 @@
603
603
  }
604
604
 
605
605
  ```
606
+
607
+
608
+
609
+ ### 追記2
610
+
611
+ そもそもコードが違いました。
612
+
613
+
614
+
615
+ var en_up = transform.up;
616
+
617
+ var en_to_pi = (playerPos.transform.position-transform.position);
618
+
619
+
620
+
621
+ ここでcosを求めないといけませんでした。
622
+
623
+ var cos = Vector3.Dot(en_up,en_to_pi)/(en_up.magnitude*en_to_pi.magnitude );
624
+
625
+ var deg = Mathf.Rad2Deg * Mathf.Acos(cos);

4

era-

2021/04/26 03:24

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -598,4 +598,8 @@
598
598
 
599
599
  }
600
600
 
601
+ }
602
+
603
+ }
604
+
601
605
  ```

3

おういえす

2021/04/25 05:52

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -458,7 +458,7 @@
458
458
 
459
459
 
460
460
 
461
-
461
+ 変更後のコード
462
462
 
463
463
  ```C#
464
464
 

2

era-NullReferenceException: Object reference not set to an instance of an object PlayerMove.Update (

2021/04/25 05:49

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -451,3 +451,151 @@
451
451
 
452
452
 
453
453
  どうか回答お願いします。
454
+
455
+
456
+
457
+ ### 追記
458
+
459
+
460
+
461
+
462
+
463
+ ```C#
464
+
465
+ using System.Collections;
466
+
467
+ using System.Collections.Generic;
468
+
469
+ using UnityEngine;
470
+
471
+
472
+
473
+ public class Enemy_Move : RandomMove
474
+
475
+ {
476
+
477
+ [SerializeField] Enemy_Status enemy; // ステータス
478
+
479
+ [SerializeField] Enemy_Collision enemyColl; // 探索範囲
480
+
481
+
482
+
483
+ Transform my_Vec; // 自身の座標
484
+
485
+ Quaternion direction; // 向き
486
+
487
+
488
+
489
+ GameObject playerPos;
490
+
491
+
492
+
493
+ private void Start()
494
+
495
+ {
496
+
497
+ playerPos = Player.player;
498
+
499
+ my_Vec = transform;
500
+
501
+ my_Vec.rotation = direction;
502
+
503
+ }
504
+
505
+ void Update()
506
+
507
+ {
508
+
509
+ var a = transform.up.normalized;
510
+
511
+ var b = (playerPos.transform.position - transform.position).normalized;
512
+
513
+
514
+
515
+ var dot = Vector3.Dot(a, b);
516
+
517
+ var rad = Mathf.Rad2Deg * dot;/*Mathf.Acos(dot)*/
518
+
519
+
520
+
521
+ if(rad <= 60 || rad >= -60) {
522
+
523
+
524
+
525
+ if (NowAct != null) {//NowActの中がnullでない場合、非追従処理が走っている可能性が高い。→非追従処理を止めて、破棄。
526
+
527
+
528
+
529
+ StopCoroutine(NowAct);
530
+
531
+ NowAct = null;
532
+
533
+ }
534
+
535
+
536
+
537
+ //print("ラジアン値"+rad);
538
+
539
+ print("視界に入っている");
540
+
541
+
542
+
543
+ if(rad != 0) {
544
+
545
+
546
+
547
+ var now_Z = transform.localRotation.eulerAngles.z;//現在角度.z
548
+
549
+ var new_Z = now_Z + (-playerPos.transform.rotation.z);//目標角度.z
550
+
551
+
552
+
553
+ float rotspeed = 3;//回転速度
554
+
555
+
556
+
557
+ float f = 0;//回転進行度
558
+
559
+
560
+
561
+ while (f <= 1) {
562
+
563
+
564
+
565
+ f += Time.deltaTime * rotspeed;
566
+
567
+ gameObject.transform.rotation = Quaternion.Euler(0, 0, Mathf.LerpAngle(now_Z, new_Z, f));
568
+
569
+ }
570
+
571
+
572
+
573
+ // 追従
574
+
575
+ my_Vec.position += (Enemy_Collision.playerPos - transform.position) * enemy.enemy_Speed;
576
+
577
+ transform.position += my_Vec.position * Time.deltaTime;
578
+
579
+ }
580
+
581
+ return;
582
+
583
+ }
584
+
585
+ else {
586
+
587
+
588
+
589
+ if (NowAct == null) {// ランダムに回転・移動
590
+
591
+
592
+
593
+ NowAct = Nolock_Action();
594
+
595
+ StartCoroutine(NowAct);
596
+
597
+ }
598
+
599
+ }
600
+
601
+ ```

1

せいや

2021/04/25 05:48

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -16,8 +16,56 @@
16
16
 
17
17
 
18
18
 
19
+
20
+
21
+ Enemy_Move
22
+
19
23
  ```C#
20
24
 
25
+ using System.Collections;
26
+
27
+ using System.Collections.Generic;
28
+
29
+ using UnityEngine;
30
+
31
+
32
+
33
+ public class Enemy_Move : RandomMove
34
+
35
+ {
36
+
37
+ [SerializeField] Enemy_Status enemy; // ステータス
38
+
39
+ [SerializeField] Enemy_Collision enemyColl; // 探索範囲
40
+
41
+
42
+
43
+ Transform my_Vec; // 自身の座標
44
+
45
+ Quaternion direction; // 向き
46
+
47
+
48
+
49
+ GameObject playerPos;
50
+
51
+
52
+
53
+ private void Start()
54
+
55
+ {
56
+
57
+ playerPos = Player.player;
58
+
59
+ my_Vec = transform;
60
+
61
+ my_Vec.rotation = direction;
62
+
63
+ }
64
+
65
+ void Update()
66
+
67
+ {
68
+
21
69
  var a = transform.up.normalized;
22
70
 
23
71
  var b = (playerPos.transform.position - transform.position).normalized;
@@ -30,14 +78,92 @@
30
78
 
31
79
 
32
80
 
33
-
81
+ if(rad <= 60 || rad >= -60) {
82
+
83
+
84
+
85
+ if (NowAct != null) {//NowActの中がnullでない場合、非追従処理が走っている可能性が高い。→非追従処理を止めて、破棄。
86
+
87
+
88
+
89
+ StopCoroutine(NowAct);
90
+
91
+ NowAct = null;
92
+
93
+ }
94
+
95
+
96
+
97
+
98
+
99
+ print("視界内");
100
+
101
+
102
+
103
+ if(rad != 0) {
104
+
105
+
106
+
107
+ var now_Z = transform.localRotation.eulerAngles.z;//現在角度.z
108
+
109
+ var new_Z = now_Z + (-playerPos.transform.rotation.z);//目標角度.z
110
+
111
+
112
+
113
+ float rotspeed = 3;//回転速度
114
+
115
+
116
+
117
+ float f = 0;//回転進行度
118
+
119
+
120
+
121
+ while (f <= 1) {
122
+
123
+
124
+
125
+ f += Time.deltaTime * rotspeed;
126
+
127
+ gameObject.transform.rotation = Quaternion.Euler(0, 0, Mathf.LerpAngle(now_Z, new_Z, f));
128
+
129
+ }
130
+
131
+
132
+
133
+ // 追従
134
+
135
+ my_Vec.position += (Enemy_Collision.playerPos - transform.position) * enemy.enemy_Speed;
136
+
137
+ transform.position += my_Vec.position * Time.deltaTime;
138
+
139
+ }
140
+
141
+ return;
142
+
143
+ }
144
+
145
+ else {
146
+
147
+
148
+
149
+ if (NowAct == null) {// ランダムに回転・移動
150
+
151
+
152
+
153
+ NowAct = Nolock_Action();
154
+
155
+ StartCoroutine(NowAct);
156
+
157
+ }
158
+
159
+ }
160
+
161
+ }
162
+
163
+ }
34
164
 
35
165
  ```
36
166
 
37
-
38
-
39
- Enemy_Move
40
-
41
167
  ```C#
42
168
 
43
169
  using System.Collections;
@@ -48,41 +174,255 @@
48
174
 
49
175
 
50
176
 
51
- public class Enemy_Move : RandomMove
177
+ public class RandomMove : MonoBehaviour
52
178
 
53
179
  {
54
180
 
55
- [SerializeField] Enemy_Status enemy; // ステータス
181
+ protected IEnumerator NowAct;
56
-
182
+
57
- [SerializeField] Enemy_Collision enemyColl; // 探索範囲
183
+ protected IEnumerator Now_Direction;
184
+
185
+
186
+
58
-
187
+ /// <summary>
188
+
59
-
189
+ /// 非捕捉状態のときの挙動
60
-
190
+
61
- Transform my_Vec; // 自身の座標
191
+ /// </summary>
62
-
192
+
63
- Quaternion direction; // 向き
193
+ protected IEnumerator Nolock_Action()
64
-
65
-
66
-
67
- GameObject playerPos;
68
-
69
-
70
-
71
- private void Start()
72
194
 
73
195
  {
74
196
 
75
- playerPos = Player.player;
76
-
77
- my_Vec = transform;
78
-
79
- my_Vec.rotation = direction;
80
-
81
- }
82
-
83
- void Update()
84
-
85
- {
197
+
198
+
199
+ var now_Z = transform.localRotation.eulerAngles.z;//現在角度.z
200
+
201
+ var new_Z = now_Z + Random.Range(60f, -60f);//目標角度.z
202
+
203
+
204
+
205
+ float rotspeed = 3;//回転速度
206
+
207
+
208
+
209
+ float f = 0;//回転進行度
210
+
211
+
212
+
213
+ while (f <= 1) {
214
+
215
+
216
+
217
+ f += Time.deltaTime * rotspeed;
218
+
219
+ gameObject.transform.rotation = Quaternion.Euler(0, 0, Mathf.LerpAngle(now_Z, new_Z, f));
220
+
221
+
222
+
223
+ yield return null;
224
+
225
+ }
226
+
227
+
228
+
229
+ float speed;// 移動スピード
230
+
231
+ float speed_Max = 4f;
232
+
233
+
234
+
235
+ float time = 0;//移動時間
236
+
237
+ float time_Max = 3;
238
+
239
+
240
+
241
+ uint move_sens = 0x00000001;
242
+
243
+
244
+
245
+ speed = Random.Range(0, speed_Max);
246
+
247
+
248
+
249
+ while (time < time_Max) {
250
+
251
+ var delta = Time.deltaTime;
252
+
253
+
254
+
255
+ time += delta;
256
+
257
+ var puls = (speed * delta) * move_sens;
258
+
259
+
260
+
261
+ transform.Translate(0, puls, 0);
262
+
263
+
264
+
265
+ yield return null;
266
+
267
+ }
268
+
269
+
270
+
271
+ NowAct = null;
272
+
273
+ }
274
+
275
+ /// <summary>
276
+
277
+ /// プレイヤーを追従
278
+
279
+ /// </summary>
280
+
281
+ /// <returns></returns>
282
+
283
+ protected IEnumerator PlayerDirection() {
284
+
285
+
286
+
287
+ var now_rot = transform.localRotation.eulerAngles.z;// 今の角度
288
+
289
+ var goal_rot = Enemy_Collision.playerPos.z;     // 変更するまでの角度
290
+
291
+
292
+
293
+ float rotSpeed = 2f;
294
+
295
+ float f = 0f;
296
+
297
+
298
+
299
+
300
+
301
+
302
+
303
+ while (f <= 1) {
304
+
305
+
306
+
307
+ f += Time.deltaTime * rotSpeed;
308
+
309
+ gameObject.transform.rotation = Quaternion.Euler(0, 0, Mathf.LerpAngle(now_rot, goal_rot, f));
310
+
311
+
312
+
313
+ Vector3 target = (Enemy_Collision.playerPos - transform.position).normalized;
314
+
315
+ if (Vector3.Dot(target, transform.forward.normalized) < -0.9) {
316
+
317
+
318
+
319
+ print("回転終了!");
320
+
321
+ break;
322
+
323
+ }
324
+
325
+ yield return null;
326
+
327
+ }
328
+
329
+
330
+
331
+ Now_Direction = null;
332
+
333
+ }
334
+
335
+ }
336
+
337
+
338
+
339
+ ```
340
+
341
+ ```C#
342
+
343
+ using System;
344
+
345
+ using System.Collections;
346
+
347
+ using System.Collections.Generic;
348
+
349
+ using UnityEngine;
350
+
351
+
352
+
353
+ [CreateAssetMenu(fileName = "Enemy", menuName = "ScriptableObjects/CreateAsset")]
354
+
355
+ public class Enemy_Status : ScriptableObject
356
+
357
+ {
358
+
359
+ public float enemy_Speed = 0.1f;// スピード
360
+
361
+ }
362
+
363
+
364
+
365
+ ```
366
+
367
+ ```C#
368
+
369
+ using System.Collections;
370
+
371
+ using System.Collections.Generic;
372
+
373
+ using UnityEngine;
374
+
375
+
376
+
377
+ public class Enemy_Collision : MonoBehaviour
378
+
379
+ {
380
+
381
+ public bool follow { get; private set; } // フォロー中か?
382
+
383
+ public static Vector3 playerPos { get; private set; }//プレイヤーの座標
384
+
385
+
386
+
387
+ private void OnTriggerStay2D(Collider2D collision) {
388
+
389
+
390
+
391
+ if (collision.CompareTag("Player")) {
392
+
393
+
394
+
395
+ follow = true;
396
+
397
+ playerPos = collision.transform.position;
398
+
399
+ }
400
+
401
+ }
402
+
403
+ private void OnTriggerExit2D(Collider2D collision) {
404
+
405
+
406
+
407
+ if (collision.CompareTag("Player")) {
408
+
409
+
410
+
411
+ follow = false;
412
+
413
+ }
414
+
415
+ }
416
+
417
+ }
418
+
419
+ ```
420
+
421
+
422
+
423
+ ↓ここで内積を求めて、視覚内に入っているかどうかを確認する。
424
+
425
+ ```C#
86
426
 
87
427
  var a = transform.up.normalized;
88
428
 
@@ -96,344 +436,18 @@
96
436
 
97
437
 
98
438
 
99
- if(rad <= 60 || rad >= -60) {
439
+
100
-
101
-
102
-
103
- if (NowAct != null) {//NowActの中がnullでない場合、非追従処理が走っている可能性が高い。→非追従処理を止めて、破棄。
104
-
105
-
106
-
107
- StopCoroutine(NowAct);
108
-
109
- NowAct = null;
110
-
111
- }
112
-
113
-
114
-
115
-
116
-
117
- print("視界に入っている");
118
-
119
-
120
-
121
- if(rad != 0) {
122
-
123
-
124
-
125
- var now_Z = transform.localRotation.eulerAngles.z;//現在角度.z
126
-
127
- var new_Z = now_Z + (-playerPos.transform.rotation.z);//目標角度.z
128
-
129
-
130
-
131
- float rotspeed = 3;//回転速度
132
-
133
-
134
-
135
- float f = 0;//回転進行度
136
-
137
-
138
-
139
- while (f <= 1) {
140
-
141
-
142
-
143
- f += Time.deltaTime * rotspeed;
144
-
145
- gameObject.transform.rotation = Quaternion.Euler(0, 0, Mathf.LerpAngle(now_Z, new_Z, f));
146
-
147
- }
148
-
149
-
150
-
151
- // 追従
152
-
153
- my_Vec.position += (Enemy_Collision.playerPos - transform.position) * enemy.enemy_Speed;
154
-
155
- transform.position += my_Vec.position * Time.deltaTime;
156
-
157
- }
158
-
159
- return;
160
-
161
- }
162
-
163
- else {
164
-
165
-
166
-
167
- if (NowAct == null) {// ランダムに回転・移動
168
-
169
-
170
-
171
- NowAct = Nolock_Action();
172
-
173
- StartCoroutine(NowAct);
174
-
175
- }
176
-
177
- }
178
-
179
- }
180
-
181
- }
182
440
 
183
441
  ```
184
442
 
185
- ```C#
443
+
186
-
187
- using System.Collections;
444
+
188
-
189
- using System.Collections.Generic;
190
-
191
- using UnityEngine;
192
-
193
-
194
-
195
- public class RandomMove : MonoBehaviour
196
-
197
- {
198
-
199
- protected IEnumerator NowAct;
200
-
201
- protected IEnumerator Now_Direction;
202
-
203
-
204
-
205
- /// <summary>
206
-
207
- /// 非捕捉状態のときの挙動
208
-
209
- /// </summary>
210
-
211
- protected IEnumerator Nolock_Action()
212
-
213
- {
214
-
215
-
216
-
217
- var now_Z = transform.localRotation.eulerAngles.z;//現在角度.z
218
-
219
- var new_Z = now_Z + Random.Range(60f, -60f);//目標角度.z
220
-
221
-
222
-
223
- float rotspeed = 3;//回転速度
224
-
225
-
226
-
227
- float f = 0;//回転進行度
228
-
229
-
230
-
231
- while (f <= 1) {
232
-
233
-
234
-
235
- f += Time.deltaTime * rotspeed;
236
-
237
- gameObject.transform.rotation = Quaternion.Euler(0, 0, Mathf.LerpAngle(now_Z, new_Z, f));
238
-
239
-
240
-
241
- yield return null;
242
-
243
- }
244
-
245
-
246
-
247
- float speed;// 移動スピード
248
-
249
- float speed_Max = 4f;
445
+ そのあと角度を見て、足りない分回転をします。
250
-
251
-
252
-
253
- float time = 0;//移動時間
446
+
254
-
255
- float time_Max = 3;
256
-
257
-
258
-
259
- uint move_sens = 0x00000001;
260
-
261
-
262
-
263
- speed = Random.Range(0, speed_Max);
264
-
265
-
266
-
267
- while (time < time_Max) {
268
-
269
- var delta = Time.deltaTime;
270
-
271
-
272
-
273
- time += delta;
274
-
275
- var puls = (speed * delta) * move_sens;
276
-
277
-
278
-
279
- transform.Translate(0, puls, 0);
280
-
281
-
282
-
283
- yield return null;
284
-
285
- }
286
-
287
-
288
-
289
- NowAct = null;
290
-
291
- }
292
-
293
- /// <summary>
294
-
295
- /// プレイヤーを追従
296
-
297
- /// </summary>
298
-
299
- /// <returns></returns>
300
-
301
- protected IEnumerator PlayerDirection() {
302
-
303
-
304
-
305
- var now_rot = transform.localRotation.eulerAngles.z;// 今の角度
306
-
307
- var goal_rot = Enemy_Collision.playerPos.z;     // 変更するまでの角度
308
-
309
-
310
-
311
- float rotSpeed = 2f;
312
-
313
- float f = 0f;
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
- while (f <= 1) {
322
-
323
-
324
-
325
- f += Time.deltaTime * rotSpeed;
326
-
327
- gameObject.transform.rotation = Quaternion.Euler(0, 0, Mathf.LerpAngle(now_rot, goal_rot, f));
328
-
329
-
330
-
331
- Vector3 target = (Enemy_Collision.playerPos - transform.position).normalized;
332
-
333
- if (Vector3.Dot(target, transform.forward.normalized) < -0.9) {
334
-
335
-
336
-
337
- print("回転終了!");
338
-
339
- break;
340
-
341
- }
342
-
343
- yield return null;
344
-
345
- }
346
-
347
-
348
-
349
- Now_Direction = null;
350
-
351
- }
352
-
353
- }
354
-
355
-
356
-
357
- ```
358
-
359
- ```C#
360
-
361
- using System;
362
-
363
- using System.Collections;
364
-
365
- using System.Collections.Generic;
366
-
367
- using UnityEngine;
368
-
369
-
370
-
371
- [CreateAssetMenu(fileName = "Enemy", menuName = "ScriptableObjects/CreateAsset")]
372
-
373
- public class Enemy_Status : ScriptableObject
374
-
375
- {
376
-
377
- public float enemy_Speed = 0.1f;// スピード
378
-
379
- }
380
-
381
-
382
-
383
- ```
384
-
385
- ```C#
386
-
387
- using System.Collections;
388
-
389
- using System.Collections.Generic;
390
-
391
- using UnityEngine;
392
-
393
-
394
-
395
- public class Enemy_Collision : MonoBehaviour
396
-
397
- {
398
-
399
- public bool follow { get; private set; } // フォロか?
447
+ そのあとに追従をするように書いたのですが、赤い範囲の中に入っていないにも関わらず、視界内とコンソルに表示されます。どうしてでしょうか?
400
-
401
- public static Vector3 playerPos { get; private set; }//プレイヤーの座標
448
+
402
-
403
-
404
-
405
- private void OnTriggerStay2D(Collider2D collision) {
406
-
407
-
408
-
409
- if (collision.CompareTag("Player")) {
410
-
411
-
412
-
413
- follow = true;
414
-
415
- playerPos = collision.transform.position;
416
-
417
- }
449
+
418
-
419
- }
450
+
420
-
421
- private void OnTriggerExit2D(Collider2D collision) {
451
+
422
-
423
-
424
-
425
- if (collision.CompareTag("Player")) {
426
-
427
-
428
-
429
- follow = false;
430
-
431
- }
432
-
433
- }
434
-
435
- }
436
-
437
- ```
438
452
 
439
453
  どうか回答お願いします。