質問編集履歴

1

状況が改善したので、編集しました

2018/01/19 04:13

投稿

jum6948
jum6948

スコア20

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  プレイヤーと一緒に戦うノンプレイヤーキャラを作っているのですが、一匹目のモンスターを倒したあと
6
6
 
7
- エラーでて何もしてくれなくなります。
7
+ 次の行動に移行するまでに時間かかります。
8
8
 
9
9
 
10
10
 
@@ -12,441 +12,393 @@
12
12
 
13
13
 
14
14
 
15
+ 2匹めの敵も攻撃するようにりましたが、Update内がうまく言っておりません。
16
+
17
+ 今は、原因究明中です。
18
+
19
+
20
+
21
+ ###該当のソースコード(2018/01/19改定)
22
+
23
+ ```C#
24
+
25
+ [Header("基本設定")]
26
+
27
+ Rigidbody rb;
28
+
29
+ AudioSource aud;
30
+
31
+
32
+
33
+ [Header("対象オブジェクト")]
34
+
35
+ GameObject targetP;
36
+
37
+ public float PlayerLength; //プレイヤーとの設定距離
38
+
39
+ public float PlayerDistance; //プレイヤーとの今の距離
40
+
41
+
42
+
43
+ public Transform target;
44
+
45
+
46
+
47
+ public Transform[] enemyList; //敵のリスト
48
+
49
+ public Transform targetE; //現在選択されている敵
50
+
51
+ public float EnemyDistance; //一番近い敵との距離
52
+
53
+
54
+
55
+ [Header("行動しているか?")]
56
+
57
+ public bool nowExecCoroutine;
58
+
59
+
60
+
61
+ [Header("再攻撃と再移動")]
62
+
63
+ //再攻撃までの時間
64
+
65
+ public float atackInterval = 0;
66
+
67
+ public float atackIntervalMax = 2.0F;
68
+
69
+
70
+
71
+ //再移動までの時間
72
+
73
+ public int moveInterval = 0;
74
+
75
+ public int TukiBunki;
76
+
77
+
78
+
79
+ //アニメーション関係
80
+
81
+ public Animator animator;
82
+
83
+
84
+
85
+ [Header("スクリプト参照")]
86
+
87
+ private EnemyState EState; //EnemyStateスクリプトへの参照
88
+
89
+ private PlayerGuardChecker PGCheck; //PlayerGuardCheckerスクリプトへの参照
90
+
91
+ private NonPcState NPState;
92
+
93
+
94
+
95
+
96
+
97
+ [Header("巡回")]
98
+
99
+ private NavMeshAgent agent;
100
+
101
+
102
+
103
+ // Use this for initialization
104
+
105
+ void Start () {
106
+
107
+ //ターゲットを取得
108
+
109
+ targetP = GameObject.FindGameObjectWithTag("Player");
110
+
111
+
112
+
113
+ //targetE = GameObject.FindGameObjectWithTag("Enemy");
114
+
115
+ atackInterval = 0;
116
+
117
+ moveInterval = 200;
118
+
119
+
120
+
121
+ //スクリプト参照
122
+
123
+ EState = GetComponent<EnemyState>();
124
+
125
+ PGCheck = GetComponent<PlayerGuardChecker>();
126
+
127
+
128
+
129
+ //音とアニメーション
130
+
131
+ animator = GetComponent<Animator>();
132
+
133
+ aud = gameObject.GetComponent<AudioSource>();
134
+
135
+ rb = GetComponent<Rigidbody>();
136
+
137
+
138
+
139
+ //初期モーション
140
+
141
+ animator.SetBool("Idle", true);
142
+
143
+
144
+
145
+ //NPCの位置チェック
146
+
147
+ //StartCoroutine(NpcPositionCheck());
148
+
149
+ }
150
+
151
+
152
+
153
+ // Update is called once per frame
154
+
155
+ void Update()
156
+
157
+ {
158
+
159
+ enemyList = GameObject.FindGameObjectsWithTag("Enemy").Select(g => g.transform).OrderBy(g => (g.position - target.position).sqrMagnitude).ToArray();
160
+
161
+
162
+
163
+ //なんらかの行動をしているか?
164
+
165
+ if (!nowExecCoroutine)
166
+
167
+ {
168
+
169
+ //プレイヤーが近くにいるか?
170
+
171
+ //いる
172
+
173
+ if (PlayerDistance >= 100)
174
+
175
+ {
176
+
177
+ //敵がいない
178
+
179
+ if (enemyList.Length == 0)
180
+
181
+ {
182
+
183
+ if (PlayerDistance > 50)
184
+
185
+ {
186
+
187
+ nowExecCoroutine = true;
188
+
189
+ StartCoroutine(NpcNormalWalk());
190
+
191
+ Debug.Log("敵がいないのでついていく");
192
+
193
+
194
+
195
+ }
196
+
197
+ else //if(Vector3.Distance(targetP.transform.position, transform.position) >= 7)
198
+
199
+ {
200
+
201
+ //すごく近いので停止
202
+
203
+ nowExecCoroutine = true;
204
+
205
+ StartCoroutine(NpcNormalKaiten());
206
+
207
+ Debug.Log("敵がいないので安心の停止");
208
+
209
+ }
210
+
211
+ }
212
+
213
+ //敵がいる
214
+
215
+ else if (enemyList.Length > 0)
216
+
217
+ {
218
+
219
+ SelectEnemy(0);
220
+
221
+ Debug.Log("tagE" + targetE + "|" + "enemyList0" + enemyList[0]);
222
+
223
+
224
+
225
+ //敵が近くにいるか?
226
+
227
+ //いる
228
+
229
+ if (EnemyDistance >= 80)//80
230
+
231
+ {
232
+
233
+ //敵がいるので抜刀
234
+
235
+ StartCoroutine(NpcKamaeIdle());
236
+
237
+ Debug.Log("構える");
238
+
239
+ }
240
+
241
+ //殴れない、近づく
242
+
243
+ else if (EnemyDistance >= 50 && EnemyDistance > 5)//50
244
+
245
+ {
246
+
247
+ nowExecCoroutine = true;
248
+
249
+ StartCoroutine(NpcKamaeWalk());
250
+
251
+ Debug.Log("敵に近づく");
252
+
253
+ }
254
+
255
+ else if (EnemyDistance <= 5)//20
256
+
257
+ {
258
+
259
+ nowExecCoroutine = true;
260
+
261
+ StartCoroutine(NpcKamaeTuki());
262
+
263
+ Debug.Log("敵につき");
264
+
265
+ }
266
+
267
+ }
268
+
269
+ }
270
+
271
+ //いないが30メートル以内にいるか?)
272
+
273
+ else if (PlayerDistance <= 110)
274
+
275
+ {
276
+
277
+ nowExecCoroutine = true;
278
+
279
+ StartCoroutine(NpcNormalKaiten());
280
+
281
+ Debug.Log("遠くから見る");
282
+
283
+ }
284
+
285
+ //いない
286
+
287
+ else
288
+
289
+ {
290
+
291
+ nowExecCoroutine = true;
292
+
293
+ StartCoroutine(NpcNormalIdle());
294
+
295
+ Debug.Log("待機");
296
+
297
+ }
298
+
299
+
300
+
301
+
302
+
303
+
304
+
305
+ }
306
+
307
+ }
308
+
309
+ //LateUpdateは、Updateが実行されたあと一回だけ実行される
310
+
311
+ void LateUpdate()
312
+
313
+ {
314
+
315
+ //Find all enemies within range and add them to list. Order list from closest to furtherest
316
+
317
+ //範囲内のすべての敵を見つけてリストに追加します。furtherestに最も近いものからの注文リスト
318
+
319
+ enemyList = GameObject.FindGameObjectsWithTag("Enemy").Select(g => g.transform).OrderBy(g => (g.position - target.position).sqrMagnitude).ToArray();
320
+
321
+
322
+
323
+ //一番近い敵との距離を算出
324
+
325
+ if (targetE != null)
326
+
327
+ {
328
+
329
+ EnemyDistance = Vector3.Distance(targetE.transform.position, transform.position);
330
+
331
+ }
332
+
333
+ //プレイヤーとの距離を算出
334
+
335
+ PlayerDistance = Vector3.Distance(targetP.transform.position, transform.position);
336
+
337
+ }
338
+
339
+
340
+
341
+ void SelectEnemy(int i)
342
+
343
+ { // 0 = Nearest / New List, 1 = Forwards, -1 = Backwards
344
+
345
+ switch (i)
346
+
347
+ {
348
+
349
+ case 0:
350
+
351
+ targetE = enemyList.FirstOrDefault();
352
+
353
+ break;
354
+
355
+ default:
356
+
357
+ targetE = enemyList[((System.Array.FindIndex(enemyList, g => g == targetE) + i) % enemyList.Length + enemyList.Length) % enemyList.Length];
358
+
359
+ break;
360
+
361
+ }
362
+
363
+ Debug.Log("敵がいる" + targetE);
364
+
365
+ }
366
+
367
+
368
+
369
+ public void NpcDead()
370
+
371
+ {
372
+
373
+ StartCoroutine(NpcResurrection());
374
+
375
+ }
376
+
377
+
378
+
379
+ void moveResaet()
380
+
381
+ {
382
+
383
+ animator.SetBool("Idle", false);
384
+
385
+ animator.SetBool("Walk", false);
386
+
387
+ animator.SetBool("Kamae", false);
388
+
389
+ animator.SetBool("Dead", false);
390
+
391
+ }
392
+
393
+
394
+
15
395
  ```
16
396
 
17
- MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
18
-
19
- Your script should either check if it is null or you should not destroy the object.
20
-
21
- NonPcKaede.Update () (at Assets/Scripts/Player/NonPcKaede.cs:110)
22
-
23
- ```
24
-
25
-
26
-
27
- ###該当のソースコード
28
-
29
- ```C#
30
-
31
- using System;
32
-
33
- using System.Collections;
34
-
35
- using System.Collections.Generic;
36
-
37
- using UnityEngine;
38
-
39
- using UnityEngine.AI;
40
-
41
-
42
-
43
- public class NonPcKaede : MonoBehaviour {
44
-
45
-
46
-
47
- [Header("基本設定")]
48
-
49
- Rigidbody rb;
50
-
51
- AudioSource aud;
52
-
53
-
54
-
55
- [Header("対象オブジェクト")]
56
-
57
- GameObject targetP;
58
-
59
- public float PlayerLength; //プレイヤーとの設定距離
60
-
61
- public float NowPlayerLength; //プレイヤーとの今の距離
62
-
63
-
64
-
65
- public Transform target;
66
-
67
-
68
-
69
- GameObject targetE;
70
-
71
- public float EnemyLength;
72
-
73
-
74
-
75
- [Header("行動しているか?")]
76
-
77
- public bool nowExecCoroutine;
78
-
79
-
80
-
81
- [Header("再攻撃と再移動")]
82
-
83
- //再攻撃までの時間
84
-
85
- public float atackInterval = 0;
86
-
87
- public float atackIntervalMax = 2.0F;
88
-
89
-
90
-
91
- //再移動までの時間
92
-
93
- public int moveInterval = 0;
94
-
95
- //プレイヤーとの距離
96
-
97
- public float CharDistance;
98
-
99
-
100
-
101
- //アニメーション関係
102
-
103
- public Animator animator;
104
-
105
-
106
-
107
- [Header("スクリプト参照")]
108
-
109
- private EnemyState EState; //EnemyStateスクリプトへの参照
110
-
111
- private PlayerGuardChecker PGCheck; //PlayerGuardCheckerスクリプトへの参照
112
-
113
-
114
-
115
- [Header("巡回")]
116
-
117
- public bool isPatrol; //巡回するかどうか
118
-
119
- public Transform[] Waypoints;
120
-
121
- private int destPoint = 0;
122
-
123
- private NavMeshAgent agent;
124
-
125
-
126
-
127
- // Use this for initialization
128
-
129
- void Start () {
130
-
131
- //ターゲットを取得
132
-
133
- targetP = GameObject.FindGameObjectWithTag("Player");
134
-
135
- targetE = GameObject.FindGameObjectWithTag("Enemy");
136
-
137
- atackInterval = 0;
138
-
139
- moveInterval = 200;
140
-
141
-
142
-
143
- //スクリプト参照
144
-
145
- EState = GetComponent<EnemyState>();
146
-
147
- PGCheck = GetComponent<PlayerGuardChecker>();
148
-
149
-
150
-
151
- //音とアニメーション
152
-
153
- animator = GetComponent<Animator>();
154
-
155
- aud = gameObject.GetComponent<AudioSource>();
156
-
157
- rb = GetComponent<Rigidbody>();
158
-
159
-
160
-
161
- //初期モーション
162
-
163
- animator.SetBool("Idle", true);
164
-
165
- }
166
-
167
-
168
-
169
- // Update is called once per frame
170
-
171
- void Update () {
172
-
173
-
174
-
175
- //プレイヤーとの距離
176
-
177
- CharDistance = Vector3.Distance(targetP.transform.position, transform.position);
178
-
179
-
180
-
181
- //なんらかの行動をしているか?
182
-
183
- if (!nowExecCoroutine)
184
-
185
- {
186
-
187
- //プレイヤーの近くにいるか?
188
-
189
- //いる
190
-
191
- if (Vector3.Distance(targetP.transform.position, transform.position) <= PlayerLength + 40)
192
-
193
- {
194
-
195
- //そもそも敵がいるか?
196
-
197
- //いない
198
-
199
- if (GameDirector.EnemyKazu == 0)
200
-
201
- {
202
-
203
- if (Vector3.Distance(targetP.transform.position, transform.position) > 20)
204
-
205
- {
206
-
207
- //距離があるので近づく
208
-
209
- nowExecCoroutine = true;
210
-
211
- StartCoroutine(NpcNormalWalk());
212
-
213
- Debug.Log("ついていく");
214
-
215
-
216
-
217
- }
218
-
219
- else //if(Vector3.Distance(targetP.transform.position, transform.position) >= 7)
220
-
221
- {
222
-
223
- //すごく近いので停止
224
-
225
- nowExecCoroutine = true;
226
-
227
- StartCoroutine(NpcNormalKaiten());
228
-
229
- Debug.Log("安心の停止");
230
-
231
- }
232
-
233
- }
234
-
235
- //いる
236
-
237
- else if (GameDirector.EnemyKazu > 0)
238
-
239
- {
240
-
241
- //敵が近くにいるか?
242
-
243
- //いる
244
-
245
- if (Vector3.Distance(targetE.transform.position, transform.position) >= EnemyLength + 50)//80
246
-
247
- {
248
-
249
- //敵がいるので抜刀
250
-
251
- StartCoroutine(NpcKamaeIdle());
252
-
253
- Debug.Log("構える");
254
-
255
-
256
-
257
- //敵は攻撃しているか?
258
-
259
- //してる
260
-
261
- //ガード
262
-
263
-
264
-
265
- //していない
266
-
267
- //抜刀
268
-
269
- //殴れるか?
270
-
271
-
272
-
273
- //殴れる
274
-
275
- //どうやって殴る?
276
-
277
-
278
-
279
- //近い
280
-
281
- //突き
282
-
283
-
284
-
285
- //中距離
286
-
287
- //払い
288
-
289
- }
290
-
291
- //殴れない、近づく
292
-
293
- else if (Vector3.Distance(targetE.transform.position, transform.position) >= EnemyLength + 30)//50
294
-
295
- {
296
-
297
- StartCoroutine(NpcKamaeWalk());
298
-
299
- Debug.Log("近づく");
300
-
301
- }
302
-
303
- else if (Vector3.Distance(targetE.transform.position, transform.position) <= EnemyLength)//50
304
-
305
- {
306
-
307
- StartCoroutine(NpcKamaeTuki());
308
-
309
- Debug.Log("つき");
310
-
311
- }
312
-
313
- //いない
314
-
315
- else
316
-
317
- {
318
-
319
- if (Vector3.Distance(targetP.transform.position, transform.position) > 30)
320
-
321
- {
322
-
323
- //距離があるので近づく
324
-
325
- nowExecCoroutine = true;
326
-
327
- StartCoroutine(NpcNormalWalk());
328
-
329
- Debug.Log("ついていく");
330
-
331
-
332
-
333
- }
334
-
335
- else //if(Vector3.Distance(targetP.transform.position, transform.position) >= 7)
336
-
337
- {
338
-
339
- //すごく近いので停止
340
-
341
- nowExecCoroutine = true;
342
-
343
- StartCoroutine(NpcNormalKaiten());
344
-
345
- Debug.Log("安心の停止");
346
-
347
- }
348
-
349
- }
350
-
351
- }
352
-
353
- }
354
-
355
- //いないが30メートル以内にいるか?)
356
-
357
- else if (Vector3.Distance(targetP.transform.position, transform.position) <= PlayerLength + 50)
358
-
359
- {
360
-
361
- nowExecCoroutine = true;
362
-
363
- StartCoroutine(NpcNormalKaiten());
364
-
365
- Debug.Log("遠くから見る");
366
-
367
- }
368
-
369
- //いない
370
-
371
- else
372
-
373
- {
374
-
375
- nowExecCoroutine = true;
376
-
377
- StartCoroutine(NpcNormalIdle());
378
-
379
- Debug.Log("待機");
380
-
381
- }
382
-
383
- }
384
-
385
- }
386
-
387
- ```
388
-
389
397
 
390
398
 
391
399
  ###試したこと
392
400
 
393
- 全ての敵に【EnemyState.cs】をアタッチしているので、それらを配列として敵が何匹いるか
401
+
394
-
395
- を取得するというやり方ができると聞いたことがあるのですが、そもそもの書き方ががわからないので
396
-
397
- 【EnemyState.cs】には【GameDirector.cs】というシステム管理してるゲームオブジェクトに情報を送るようにしました。
398
-
399
- ```
400
-
401
- void start
402
-
403
- {
404
-
405
- //GameDirectorのエネミーリストへ追加
406
-
407
- GameDirector.EnemyKazu++;
408
-
409
- }
410
-
411
- // 破棄されるとき
412
-
413
- void OnDestory()
414
-
415
- {
416
-
417
- //GameDirectorのエネミーリストから除外する
418
-
419
- GameDirector.EnemyKazu =-1;
420
-
421
- }
422
-
423
- ```
424
-
425
- 敵がいるか、いないかは、判別できるようになりましたが、複数の敵には対応できません。
426
-
427
-
428
-
429
- > static List<Pauser> targets = new List<Pauser>(); // ポーズ対象のスクリプト
430
-
431
-
432
-
433
- これは、全然違うスクリプトですが、なんかこういう感じでリストを作ればいいと見ましたが、記述方法がわかりません。見よう見まねで、
434
-
435
- ```
436
-
437
- public static List<EnemyState> targets = new List<EnemyState>();
438
-
439
-
440
-
441
- void update
442
-
443
- {Debug.Log("敵の数" targets)}
444
-
445
- ```
446
-
447
- こんな感じで書いてみたものの、数がわかるようになっただけでどうにもこうにもできません。
448
-
449
- 以上が試したことになります。他にもとんちんかんなことはやってますが、よく覚えていません。
450
402
 
451
403
 
452
404
 
@@ -461,9 +413,3 @@
461
413
  MicrosoftVisualStudio
462
414
 
463
415
  上記が作成環境になっております。
464
-
465
-
466
-
467
- あまりに長いので今回はいれませんでしたが、他のコードの情報がいる場合はコメントお願い致します。
468
-
469
- なるべく早く用意させて頂きます。