質問編集履歴

4

プログラムの変更

2018/05/05 16:45

投稿

uepon
uepon

スコア17

test CHANGED
@@ -1 +1 @@
1
- prefabにBox Collider 2Dnull困っています
1
+ 複数敵キャラの中自機から弾が出当たると敵キャラが消えるソースを作りたい。
test CHANGED
@@ -1,480 +1,12 @@
1
1
  Unity 初心者です。
2
2
 
3
- Unity に、prefabにBox Collider 2D設定をすると、動きおかしくな困ってます
3
+ 複数の敵キャラの中で自機から弾が出当たると敵キャラ消えるソースを作たい
4
4
 
5
-
6
-
7
- ```ここに言語を入力
8
-
9
- コード
10
-
11
- using System.Collections;
12
-
13
- using System.Collections.Generic;
14
-
15
- using UnityEngine;
16
-
17
- using System;
18
-
19
-
20
-
21
-
22
-
23
- static class GlobalVariables
24
-
25
- {
26
-
27
- static public bool Shoot_Flag = false;
28
-
29
- }
30
-
31
-
32
-
33
- [System.Serializable]
34
-
35
- public struct Human_Status
36
-
37
- {
38
-
39
- public GameObject Human; //敵キャラのx,y座標
40
-
41
- public int Dir; //敵キャラの横移動、縦移動
42
-
43
- public int Atack_Counter; //敵キャラの縦移動を決めるカウンター
44
-
45
- public int T_Counter; //敵キャラのタイミングカウンター
46
-
47
- public float Ho_x; //敵キャラの出現位置
48
-
49
- public float Ho_y; //敵キャラの出現位置
50
-
51
-
52
-
53
- }
54
-
55
-
56
-
57
- public class Deseart_Hyouji : MonoBehaviour
58
-
59
- {
60
-
61
- private GameObject[,] Desert_obj;
62
-
63
- }
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
- public class Desert_load5 : MonoBehaviour
72
-
73
- {
74
-
75
- public int HUMAN_num = 7;
76
-
77
- public List<Human_Status> Chara = new List<Human_Status>();
78
-
79
- public Human_Status human_status = new Human_Status();
80
-
81
- private GameObject Trump_obj;
82
-
83
- private float tx = 0.0f;
84
-
85
- private float ty = -2.75f;
86
-
87
- private float tz = 0.00f;
88
-
89
-
90
-
91
- private Vector3 pos;
92
-
93
- private Vector3 WorldPointPos;
94
-
95
- private float point_x = 0.0f;
96
-
97
- private float point_y = 0.0f;
98
-
99
- private float rad;
100
-
101
- private float dx;
102
-
103
- private float dy;
104
-
105
- private float r_dx;
106
-
107
- private float r_dy;
108
-
109
- private float S_x;
110
-
111
- private float S_y;
112
-
113
-
114
-
115
- public GameObject Shot_obj;
116
-
117
- public GameObject Taget_obj;
118
-
119
- private int rote_z = 0;
120
-
121
-
122
-
123
- void Start()
124
-
125
- {
126
-
127
- Deseart_Hyouji hyouji = new Deseart_Hyouji();
128
-
129
- hyouji.Hyouji();
130
-
131
- GameObject Mexico_Man_obj = new GameObject();
132
-
133
- human_status.Human = Mexico_Man_obj;
134
-
135
- human_status.Human.name = "Mexico_Man";
136
-
137
- for (int k = 0; k < HUMAN_num; k++)
138
-
139
- {
140
-
141
- Vector2 tmp2 = GameObject.Find("Mexico_Man").transform.position;
142
-
143
- GameObject.Find("Mexico_Man").transform.position = new Vector2(UnityEngine.Random.Range(-1.0f, 2.3f), UnityEngine.Random.Range(1.8f, 4.6f));
144
-
145
- tmp2.x = UnityEngine.Random.Range(-1.0f, 2.3f);
146
-
147
- tmp2.y = UnityEngine.Random.Range(1.8f, 4.6f);
148
-
149
- Mexico_Man_obj = Instantiate(Resources.Load("Mexicoman_pref"), new Vector2(tmp2.x, tmp2.y), Quaternion.identity) as GameObject;
150
-
151
- human_status.Human = Mexico_Man_obj;
152
-
153
- human_status.Dir = UnityEngine.Random.Range(1, 3);
154
-
155
- human_status.T_Counter = 0;
156
-
157
- human_status.Ho_x = 0.00f;
158
-
159
- human_status.Ho_y = 0.00f;
160
-
161
-
162
-
163
- Chara.Add(human_status);
164
-
165
- }
166
-
167
- Trump_obj = Instantiate(Resources.Load("trump_pref"), new Vector3(tx, ty, tz), Quaternion.identity) as GameObject;
168
-
169
- Trump_obj.name = "Trump";
170
-
171
- }
172
-
173
-
174
-
175
- void Update()
176
-
177
- {
178
-
179
- for (int i = 0; i < HUMAN_num; i++)
180
-
181
- {
182
-
183
- Human_Status human_status = Chara[i];
184
-
185
-
186
-
187
- Vector3 pos = human_status.Human.transform.position;
188
-
189
- human_status.T_Counter = human_status.T_Counter + 1;
190
-
191
-
192
-
193
- if (human_status.T_Counter > 5)
194
-
195
- {
196
-
197
- human_status.T_Counter = 0;
198
-
199
- human_status.Atack_Counter = UnityEngine.Random.Range(1, 9);
5
+ Unity にて、prefabにBox Collider 2D設定し下記ソースを、敵キャラクターPrefabに設定したのですが、
200
-
201
-
202
-
203
- if (human_status.Atack_Counter == 6)
204
-
205
- {
206
-
207
- human_status.Dir = 3;
208
-
209
- }
210
-
211
-
212
-
213
- }
214
-
215
- if (human_status.Dir == 1)
216
-
217
- {
218
-
219
-
220
-
221
- pos.x = pos.x - 0.05f;
222
-
223
- human_status.Human.transform.position = pos;
224
-
225
- if (human_status.Human.transform.position.x < -1.62f)
226
-
227
- {
228
-
229
- human_status.Dir = 2;
230
-
231
- }
232
-
233
- else if ((human_status.Atack_Counter == 7) || (human_status.Atack_Counter == 8))
234
-
235
- {
236
-
237
- human_status.Dir = 2;
238
-
239
- human_status.Atack_Counter = 0;
240
-
241
- }
242
-
243
- }
244
-
245
- else if (human_status.Dir == 2)
246
-
247
- {
248
-
249
- pos.x = pos.x + 0.05f;
250
-
251
- human_status.Human.transform.position = pos;
252
-
253
- if (human_status.Human.transform.position.x > 2.3f)
254
-
255
- {
256
-
257
- human_status.Dir = 1;
258
-
259
- }
260
-
261
- else if ((human_status.Atack_Counter == 1) || (human_status.Atack_Counter == 2))
262
-
263
- {
264
-
265
- human_status.Dir = 1;
266
-
267
- human_status.Atack_Counter = 0;
268
-
269
- }
270
-
271
- }
272
-
273
- else if (human_status.Dir == 3)
274
-
275
- {
276
-
277
- if (human_status.Human.transform.position.y < -5.0f)
278
-
279
- {
280
-
281
- human_status.Ho_x = UnityEngine.Random.Range(-1.0f, 2.3f);//最小-1.2f(-1.0f)最大(2.5f)2.3f
282
-
283
- human_status.Human.transform.position = new Vector3(human_status.Ho_x, 5.0f, 0.00f);
284
-
285
- }
286
-
287
- else if (human_status.Human.transform.position.y > 4.6f)
288
-
289
- {
290
-
291
- human_status.Human.transform.position = new Vector3(human_status.Human.transform.position.x, human_status.Human.transform.position.y + -0.05f, 0.00f);
292
-
293
- }
294
-
295
-
296
-
297
- else
298
-
299
- {
300
-
301
- human_status.Human.transform.position = new Vector3(human_status.Human.transform.position.x, human_status.Human.transform.position.y + -0.05f, 0.00f);
302
-
303
- human_status.Ho_y = UnityEngine.Random.Range(1.8f, 4.6f);//最小1.8f最大4.6f
304
-
305
- if (human_status.Ho_y <= 4.6f)
306
-
307
- {
308
-
309
- if ((human_status.Ho_y >= 1.8f) | (human_status.Ho_y <= 4.6f))
310
-
311
- {
312
-
313
- if (human_status.Human.transform.position.y > human_status.Ho_y)
314
-
315
- {
316
-
317
- human_status.Dir = UnityEngine.Random.Range(1, 4);
318
-
319
- }
320
-
321
- }
322
-
323
-
324
-
325
- }
326
-
327
- }
328
-
329
-
330
-
331
- }
332
-
333
- Chara[i] = human_status;
334
-
335
- }
336
-
337
- if (Input.GetMouseButtonDown(0) && GlobalVariables.Shoot_Flag == false)
338
-
339
- {
340
-
341
-
342
-
343
- //マウス位置座標をVector3で取得
344
-
345
- pos = Input.mousePosition;
346
-
347
- // マウス位置座標をスクリーン座標からワールド座標に変換する
348
-
349
- WorldPointPos = Camera.main.ScreenToWorldPoint(pos);
350
-
351
- point_x = WorldPointPos.x;
352
-
353
- point_y = WorldPointPos.y;
354
-
355
-
356
-
357
- if (point_x < -1.39f)
358
-
359
- {
360
-
361
- point_x = -1.35f;
362
-
363
-
364
-
365
- }
366
-
367
- if (point_y < -4.25f)
368
-
369
- {
370
-
371
- point_y = -4.25f;
372
-
373
-
374
-
375
- }
376
-
377
-
378
-
379
- //trumpの座標を取る。
380
-
381
- Vector3 tmp3 = GameObject.Find("Trump").transform.position;
382
-
383
- GameObject.Find("Trump").transform.position = new Vector3(tmp3.x, tmp3.y, tmp3.z);
384
-
385
-
386
-
387
- dx = tmp3.x;
388
-
389
- dy = tmp3.y;
390
-
391
- r_dx = point_x - dx;
392
-
393
- r_dy = point_y - dy;
394
-
395
- rad = Mathf.Atan2(r_dy, r_dx);
396
-
397
-
398
-
399
- Shot_obj = Instantiate(Resources.Load("Frying_pan_pref"), new Vector3(dx, dy, 0.0f), Quaternion.identity) as GameObject;
400
-
401
- Taget_obj = Instantiate(Resources.Load("taget3_pref"), new Vector3(point_x, point_y, 0.0f), Quaternion.identity) as GameObject;
402
-
403
- Shot_obj.transform.Rotate(new Vector3(0, 0, rad * 45.0f));
404
-
405
- GlobalVariables.Shoot_Flag = true;
406
-
407
- }
408
-
409
- else if(GlobalVariables.Shoot_Flag == true)
410
-
411
- {
412
-
413
- Vector3 Position = Shot_obj.transform.position;
414
-
415
- float xx = Mathf.Cos(rad);
416
-
417
- float yy = Mathf.Sin(rad);
418
-
419
- xx *= -0.1f;//idatenn
420
-
421
- yy *= -0.1f;//idatenn
422
-
423
- Position.x -= xx; //* Time.deltaTime ;
424
-
425
- Position.y -= yy; //* Time.deltaTime;
426
-
427
- Debug.Log("xx=" + xx);
428
-
429
- Debug.Log("yy=" + yy);
430
-
431
-
432
-
433
- S_x = Position.x;
434
-
435
- S_y = Position.y;
436
-
437
- Shot_obj.transform.position = new Vector3(S_x, S_y, 0.0f);
438
-
439
-
440
-
441
- // Shot_obj.transform.Rotate(new Vector3(0, 0, rote_z));
442
-
443
- // rote_z = rote_z + 30;
444
-
445
-
446
-
447
- if (rote_z == 360)
448
-
449
- {
450
-
451
- rote_z = 0;
452
-
453
- }
454
-
455
- }
456
-
457
- }
458
-
459
- }
460
6
 
461
7
 
462
8
 
463
9
  ```
464
-
465
-
466
-
467
- "Mexicoman_pref"にBox Collider 2D設定をすると、動きがおかしくなり困ってます。
468
-
469
-
470
-
471
- Box Collider 2Dと下記スクリプトを追加すると動きは、おかしくならないようになったのですが、
472
-
473
- コンソールに代わりにnullで止ま表示され止まり動きが止まるキャラクタが出ます。
474
-
475
- 解決方法をアドバイス下さい。
476
-
477
- ```ここに言語を入力
478
10
 
479
11
  using System.Collections;
480
12
 
@@ -492,29 +24,23 @@
492
24
 
493
25
 
494
26
 
495
-
496
-
497
-
498
-
499
-
500
-
501
27
  // Use this for initialization
502
28
 
503
29
  void Start () {
504
30
 
505
31
 
506
32
 
507
- }
33
+   }
508
34
 
509
35
 
510
36
 
511
- // Update is called once per frame
37
+   // Update is called once per frame
512
38
 
513
- void Update () {
39
+   void Update () {
514
40
 
515
-
41
+   
516
42
 
517
- }
43
+   }
518
44
 
519
45
  void OnTriggerEnter2D(Collider2D c)
520
46
 
@@ -524,23 +50,35 @@
524
50
 
525
51
  {
526
52
 
527
- Destroy(M_obj);
53
+ GameObject.Destroy(M_obj);
528
54
 
529
-
55
+ M_obj = null;
530
56
 
531
57
  }
532
58
 
59
+ if (M_obj == null)
533
60
 
61
+ {
534
62
 
63
+ Debug.Log("hoge hoge");
64
+
65
+ }
66
+
67
+ if (M_obj != null)
68
+
69
+ {
70
+
71
+ Debug.Log("homo homo");
72
+
73
+ }
74
+
535
- Debug.Log("hit2 Object COLLISION");
75
+ Debug.Log("hit3 Object COLLISION");
536
76
 
537
77
  Debug.Log("M_obj " + M_obj);
538
78
 
539
79
  return;
540
80
 
541
81
  }
542
-
543
-
544
82
 
545
83
  }
546
84
 
@@ -549,3 +87,43 @@
549
87
  コード
550
88
 
551
89
  ```
90
+
91
+ エラーコードから、
92
+
93
+ MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
94
+
95
+ Your script should either check if it is null or you should not destroy the object.
96
+
97
+ Desert_load5.Update () (at Assets/Desert_load5.cs:151)
98
+
99
+ 該当行で、
100
+
101
+
102
+
103
+ ``` void Update()
104
+
105
+ {
106
+
107
+ for (int i = 0; i < HUMAN_num; i++)
108
+
109
+ {
110
+
111
+ Human_Status human_status = Chara[i];
112
+
113
+
114
+
115
+ Vector3 pos = human_status.Human.transform.position;//151行目
116
+
117
+ human_status.T_Counter = human_status.T_Counter + 1;
118
+
119
+
120
+
121
+
122
+
123
+ コード
124
+
125
+ ```
126
+
127
+ プレファブをnullにした後、どのように振るまっていいのか解りません。
128
+
129
+ アドバイスの程、よろしくお願いいたします。

3

Box Collider 2Dと下記スクリプトを追加しまた。

2018/05/05 16:45

投稿

uepon
uepon

スコア17

test CHANGED
@@ -1 +1 @@
1
- prefabにBox Collider 2D設定をると動きがおかしくなる
1
+ prefabにBox Collider 2Dのnullで困っています。
test CHANGED
@@ -24,10 +24,6 @@
24
24
 
25
25
  {
26
26
 
27
-
28
-
29
-
30
-
31
27
  static public bool Shoot_Flag = false;
32
28
 
33
29
  }
@@ -58,480 +54,498 @@
58
54
 
59
55
 
60
56
 
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
57
  public class Deseart_Hyouji : MonoBehaviour
70
58
 
71
59
  {
72
60
 
73
61
  private GameObject[,] Desert_obj;
74
62
 
75
-
63
+ }
64
+
65
+
66
+
67
+
68
+
69
+
70
+
76
-
71
+ public class Desert_load5 : MonoBehaviour
72
+
73
+ {
74
+
75
+ public int HUMAN_num = 7;
76
+
77
+ public List<Human_Status> Chara = new List<Human_Status>();
78
+
79
+ public Human_Status human_status = new Human_Status();
80
+
81
+ private GameObject Trump_obj;
82
+
83
+ private float tx = 0.0f;
84
+
85
+ private float ty = -2.75f;
86
+
87
+ private float tz = 0.00f;
88
+
89
+
90
+
91
+ private Vector3 pos;
92
+
93
+ private Vector3 WorldPointPos;
94
+
95
+ private float point_x = 0.0f;
96
+
97
+ private float point_y = 0.0f;
98
+
99
+ private float rad;
100
+
101
+ private float dx;
102
+
103
+ private float dy;
104
+
105
+ private float r_dx;
106
+
107
+ private float r_dy;
108
+
109
+ private float S_x;
110
+
111
+ private float S_y;
112
+
113
+
114
+
115
+ public GameObject Shot_obj;
116
+
117
+ public GameObject Taget_obj;
118
+
119
+ private int rote_z = 0;
120
+
121
+
122
+
123
+ void Start()
124
+
125
+ {
126
+
127
+ Deseart_Hyouji hyouji = new Deseart_Hyouji();
128
+
77
- public void Hyouji()
129
+ hyouji.Hyouji();
130
+
131
+ GameObject Mexico_Man_obj = new GameObject();
132
+
133
+ human_status.Human = Mexico_Man_obj;
134
+
135
+ human_status.Human.name = "Mexico_Man";
136
+
137
+ for (int k = 0; k < HUMAN_num; k++)
78
138
 
79
139
  {
80
140
 
141
+ Vector2 tmp2 = GameObject.Find("Mexico_Man").transform.position;
142
+
143
+ GameObject.Find("Mexico_Man").transform.position = new Vector2(UnityEngine.Random.Range(-1.0f, 2.3f), UnityEngine.Random.Range(1.8f, 4.6f));
144
+
145
+ tmp2.x = UnityEngine.Random.Range(-1.0f, 2.3f);
146
+
147
+ tmp2.y = UnityEngine.Random.Range(1.8f, 4.6f);
148
+
149
+ Mexico_Man_obj = Instantiate(Resources.Load("Mexicoman_pref"), new Vector2(tmp2.x, tmp2.y), Quaternion.identity) as GameObject;
150
+
151
+ human_status.Human = Mexico_Man_obj;
152
+
153
+ human_status.Dir = UnityEngine.Random.Range(1, 3);
154
+
155
+ human_status.T_Counter = 0;
156
+
157
+ human_status.Ho_x = 0.00f;
158
+
159
+ human_status.Ho_y = 0.00f;
160
+
161
+
162
+
163
+ Chara.Add(human_status);
164
+
165
+ }
166
+
167
+ Trump_obj = Instantiate(Resources.Load("trump_pref"), new Vector3(tx, ty, tz), Quaternion.identity) as GameObject;
168
+
81
- Desert_obj = new GameObject[16, 9];
169
+ Trump_obj.name = "Trump";
170
+
82
-
171
+ }
83
-
84
-
172
+
173
+
174
+
85
- for (int i = 0; i < 16; i++)
175
+ void Update()
86
-
176
+
87
- {
177
+ {
88
-
89
-
90
-
178
+
91
- for (int j = 0; j < 9; j++)
179
+ for (int i = 0; i < HUMAN_num; i++)
92
-
180
+
93
- {
181
+ {
182
+
94
-
183
+ Human_Status human_status = Chara[i];
184
+
185
+
186
+
187
+ Vector3 pos = human_status.Human.transform.position;
188
+
189
+ human_status.T_Counter = human_status.T_Counter + 1;
190
+
191
+
192
+
95
- if ((i == 5) /*& (j >= 2)*/)
193
+ if (human_status.T_Counter > 5)
194
+
195
+ {
196
+
197
+ human_status.T_Counter = 0;
198
+
199
+ human_status.Atack_Counter = UnityEngine.Random.Range(1, 9);
200
+
201
+
202
+
203
+ if (human_status.Atack_Counter == 6)
204
+
205
+ {
206
+
207
+ human_status.Dir = 3;
208
+
209
+ }
210
+
211
+
212
+
213
+ }
214
+
215
+ if (human_status.Dir == 1)
216
+
217
+ {
218
+
219
+
220
+
221
+ pos.x = pos.x - 0.05f;
222
+
223
+ human_status.Human.transform.position = pos;
224
+
225
+ if (human_status.Human.transform.position.x < -1.62f)
226
+
227
+ {
228
+
229
+ human_status.Dir = 2;
230
+
231
+ }
232
+
233
+ else if ((human_status.Atack_Counter == 7) || (human_status.Atack_Counter == 8))
234
+
235
+ {
236
+
237
+ human_status.Dir = 2;
238
+
239
+ human_status.Atack_Counter = 0;
240
+
241
+ }
242
+
243
+ }
244
+
245
+ else if (human_status.Dir == 2)
246
+
247
+ {
248
+
249
+ pos.x = pos.x + 0.05f;
250
+
251
+ human_status.Human.transform.position = pos;
252
+
253
+ if (human_status.Human.transform.position.x > 2.3f)
254
+
255
+ {
256
+
257
+ human_status.Dir = 1;
258
+
259
+ }
260
+
261
+ else if ((human_status.Atack_Counter == 1) || (human_status.Atack_Counter == 2))
262
+
263
+ {
264
+
265
+ human_status.Dir = 1;
266
+
267
+ human_status.Atack_Counter = 0;
268
+
269
+ }
270
+
271
+ }
272
+
273
+ else if (human_status.Dir == 3)
274
+
275
+ {
276
+
277
+ if (human_status.Human.transform.position.y < -5.0f)
278
+
279
+ {
280
+
281
+ human_status.Ho_x = UnityEngine.Random.Range(-1.0f, 2.3f);//最小-1.2f(-1.0f)最大(2.5f)2.3f
282
+
283
+ human_status.Human.transform.position = new Vector3(human_status.Ho_x, 5.0f, 0.00f);
284
+
285
+ }
286
+
287
+ else if (human_status.Human.transform.position.y > 4.6f)
288
+
289
+ {
290
+
291
+ human_status.Human.transform.position = new Vector3(human_status.Human.transform.position.x, human_status.Human.transform.position.y + -0.05f, 0.00f);
292
+
293
+ }
294
+
295
+
296
+
297
+ else
298
+
299
+ {
300
+
301
+ human_status.Human.transform.position = new Vector3(human_status.Human.transform.position.x, human_status.Human.transform.position.y + -0.05f, 0.00f);
302
+
303
+ human_status.Ho_y = UnityEngine.Random.Range(1.8f, 4.6f);//最小1.8f最大4.6f
304
+
305
+ if (human_status.Ho_y <= 4.6f)
96
306
 
97
307
  {
98
308
 
99
- Desert_obj[i, j] = Instantiate(Resources.Load("Border_pref"), new Vector2(-2.9f + (0.645f * j), 5.0f - (0.645f * i)), Quaternion.identity) as GameObject;
309
+ if ((human_status.Ho_y >= 1.8f) | (human_status.Ho_y <= 4.6f))
310
+
311
+ {
312
+
313
+ if (human_status.Human.transform.position.y > human_status.Ho_y)
314
+
315
+ {
316
+
317
+ human_status.Dir = UnityEngine.Random.Range(1, 4);
318
+
319
+ }
320
+
321
+ }
322
+
323
+
100
324
 
101
325
  }
102
326
 
103
- // else if (j <= 1)
104
-
105
- // {
106
-
107
- // Desert_obj[i, j] = Instantiate(Resources.Load("b_choice"), new Vector2(-2.9f + (0.64f * j), 5.0f - (0.64f * i)), Quaternion.identity) as GameObject;
108
-
109
- // }
110
-
111
- else if (i >= 13)
112
-
113
- {
114
-
115
- Desert_obj[i, j] = Instantiate(Resources.Load("b_choice"), new Vector2(-2.9f + (0.64f * j), 5.0f - (0.64f * i)), Quaternion.identity) as GameObject;
116
-
117
- }
327
+ }
118
-
119
- else
328
+
120
-
121
- {
329
+
122
-
123
- Desert_obj[i, j] = Instantiate(Resources.Load("Desert_pref"), new Vector2(-2.9f + (0.645f * j), 5.0f - (0.645f * i)), Quaternion.identity) as GameObject;
330
+
124
-
125
- }
331
+ }
126
-
332
+
127
- }
333
+ Chara[i] = human_status;
128
-
129
- }
130
334
 
131
335
  }
132
336
 
337
+ if (Input.GetMouseButtonDown(0) && GlobalVariables.Shoot_Flag == false)
338
+
339
+ {
340
+
341
+
342
+
343
+ //マウス位置座標をVector3で取得
344
+
345
+ pos = Input.mousePosition;
346
+
347
+ // マウス位置座標をスクリーン座標からワールド座標に変換する
348
+
349
+ WorldPointPos = Camera.main.ScreenToWorldPoint(pos);
350
+
351
+ point_x = WorldPointPos.x;
352
+
353
+ point_y = WorldPointPos.y;
354
+
355
+
356
+
357
+ if (point_x < -1.39f)
358
+
359
+ {
360
+
361
+ point_x = -1.35f;
362
+
363
+
364
+
365
+ }
366
+
367
+ if (point_y < -4.25f)
368
+
369
+ {
370
+
371
+ point_y = -4.25f;
372
+
373
+
374
+
375
+ }
376
+
377
+
378
+
379
+ //trumpの座標を取る。
380
+
381
+ Vector3 tmp3 = GameObject.Find("Trump").transform.position;
382
+
383
+ GameObject.Find("Trump").transform.position = new Vector3(tmp3.x, tmp3.y, tmp3.z);
384
+
385
+
386
+
387
+ dx = tmp3.x;
388
+
389
+ dy = tmp3.y;
390
+
391
+ r_dx = point_x - dx;
392
+
393
+ r_dy = point_y - dy;
394
+
395
+ rad = Mathf.Atan2(r_dy, r_dx);
396
+
397
+
398
+
399
+ Shot_obj = Instantiate(Resources.Load("Frying_pan_pref"), new Vector3(dx, dy, 0.0f), Quaternion.identity) as GameObject;
400
+
401
+ Taget_obj = Instantiate(Resources.Load("taget3_pref"), new Vector3(point_x, point_y, 0.0f), Quaternion.identity) as GameObject;
402
+
403
+ Shot_obj.transform.Rotate(new Vector3(0, 0, rad * 45.0f));
404
+
405
+ GlobalVariables.Shoot_Flag = true;
406
+
407
+ }
408
+
409
+ else if(GlobalVariables.Shoot_Flag == true)
410
+
411
+ {
412
+
413
+ Vector3 Position = Shot_obj.transform.position;
414
+
415
+ float xx = Mathf.Cos(rad);
416
+
417
+ float yy = Mathf.Sin(rad);
418
+
419
+ xx *= -0.1f;//idatenn
420
+
421
+ yy *= -0.1f;//idatenn
422
+
423
+ Position.x -= xx; //* Time.deltaTime ;
424
+
425
+ Position.y -= yy; //* Time.deltaTime;
426
+
427
+ Debug.Log("xx=" + xx);
428
+
429
+ Debug.Log("yy=" + yy);
430
+
431
+
432
+
433
+ S_x = Position.x;
434
+
435
+ S_y = Position.y;
436
+
437
+ Shot_obj.transform.position = new Vector3(S_x, S_y, 0.0f);
438
+
439
+
440
+
441
+ // Shot_obj.transform.Rotate(new Vector3(0, 0, rote_z));
442
+
443
+ // rote_z = rote_z + 30;
444
+
445
+
446
+
447
+ if (rote_z == 360)
448
+
449
+ {
450
+
451
+ rote_z = 0;
452
+
453
+ }
454
+
455
+ }
456
+
133
457
  }
134
458
 
135
-
136
-
137
-
138
-
139
-
140
-
141
- public class Desert_load5 : MonoBehaviour
142
-
143
- {
144
-
145
- public int HUMAN_num = 7;
146
-
147
- public List<Human_Status> Chara = new List<Human_Status>();
148
-
149
- public Human_Status human_status = new Human_Status();
150
-
151
- private GameObject Trump_obj;
152
-
153
- private float tx = 0.0f;
154
-
155
- private float ty = -2.75f;
156
-
157
- private float tz = 0.00f;
158
-
159
-
160
-
161
- private Vector3 pos;
162
-
163
- private Vector3 WorldPointPos;
164
-
165
- private float point_x = 0.0f;
166
-
167
- private float point_y = 0.0f;
168
-
169
- private float rad;
170
-
171
- private float dx;
172
-
173
- private float dy;
174
-
175
- private float r_dx;
176
-
177
- private float r_dy;
178
-
179
- private float S_x;
180
-
181
- private float S_y;
182
-
183
-
184
-
185
- public GameObject Shot_obj;
186
-
187
- public GameObject Taget_obj;
188
-
189
- private int rote_z = 0;
190
-
191
-
192
-
193
- void Start()
459
+ }
460
+
461
+
462
+
463
+ ```
464
+
465
+
466
+
467
+ "Mexicoman_pref"にBox Collider 2D設定をすると、動きがおかしくなり困ってます。
468
+
469
+
470
+
471
+ Box Collider 2Dと下記スクリプトを追加すると動きは、おかしくならないようになったのですが、
472
+
473
+ コンソールに代わりにnullで止ま表示され止まり動きが止まるキャラクタが出ます。
474
+
475
+ 解決方法をアドバイス下さい。
476
+
477
+ ```ここに言語を入力
478
+
479
+ using System.Collections;
480
+
481
+ using System.Collections.Generic;
482
+
483
+ using UnityEngine;
484
+
485
+
486
+
487
+ public class Trigger3 : MonoBehaviour {
488
+
489
+
490
+
491
+ public GameObject M_obj;
492
+
493
+
494
+
495
+
496
+
497
+
498
+
499
+
500
+
501
+ // Use this for initialization
502
+
503
+ void Start () {
504
+
505
+
506
+
507
+ }
508
+
509
+
510
+
511
+ // Update is called once per frame
512
+
513
+ void Update () {
514
+
515
+
516
+
517
+ }
518
+
519
+ void OnTriggerEnter2D(Collider2D c)
194
520
 
195
521
  {
196
522
 
197
- Deseart_Hyouji hyouji = new Deseart_Hyouji();
198
-
199
- hyouji.Hyouji();
200
-
201
- GameObject Mexico_Man_obj = new GameObject();
202
-
203
- human_status.Human = Mexico_Man_obj;
204
-
205
- human_status.Human.name = "Mexico_Man";
523
+ if (c.gameObject.tag == "Hit")
206
-
207
- for (int k = 0; k < HUMAN_num; k++)
208
524
 
209
525
  {
210
526
 
211
- Vector2 tmp2 = GameObject.Find("Mexico_Man").transform.position;
212
-
213
- GameObject.Find("Mexico_Man").transform.position = new Vector2(UnityEngine.Random.Range(-1.0f, 2.3f), UnityEngine.Random.Range(1.8f, 4.6f));
214
-
215
- tmp2.x = UnityEngine.Random.Range(-1.0f, 2.3f);
216
-
217
- tmp2.y = UnityEngine.Random.Range(1.8f, 4.6f);
218
-
219
- Mexico_Man_obj = Instantiate(Resources.Load("Mexicoman_pref"), new Vector2(tmp2.x, tmp2.y), Quaternion.identity) as GameObject;
220
-
221
- human_status.Human = Mexico_Man_obj;
222
-
223
- human_status.Dir = UnityEngine.Random.Range(1, 3);
224
-
225
- human_status.T_Counter = 0;
226
-
227
- human_status.Ho_x = 0.00f;
228
-
229
- human_status.Ho_y = 0.00f;
527
+ Destroy(M_obj);
230
-
231
-
232
-
233
- Chara.Add(human_status);
528
+
529
+
234
530
 
235
531
  }
236
532
 
533
+
534
+
237
- Trump_obj = Instantiate(Resources.Load("trump_pref"), new Vector3(tx, ty, tz), Quaternion.identity) as GameObject;
535
+ Debug.Log("hit2 Object COLLISION");
238
-
536
+
239
- Trump_obj.name = "Trump";
537
+ Debug.Log("M_obj " + M_obj);
538
+
539
+ return;
240
540
 
241
541
  }
242
542
 
243
543
 
244
544
 
245
- void Update()
246
-
247
- {
248
-
249
- for (int i = 0; i < HUMAN_num; i++)
250
-
251
- {
252
-
253
- Human_Status human_status = Chara[i];
254
-
255
-
256
-
257
- Vector3 pos = human_status.Human.transform.position;
258
-
259
- human_status.T_Counter = human_status.T_Counter + 1;
260
-
261
-
262
-
263
- if (human_status.T_Counter > 5)
264
-
265
- {
266
-
267
- human_status.T_Counter = 0;
268
-
269
- human_status.Atack_Counter = UnityEngine.Random.Range(1, 9);
270
-
271
-
272
-
273
- if (human_status.Atack_Counter == 6)
274
-
275
- {
276
-
277
- human_status.Dir = 3;
278
-
279
- }
280
-
281
-
282
-
283
- }
284
-
285
- if (human_status.Dir == 1)
286
-
287
- {
288
-
289
-
290
-
291
- pos.x = pos.x - 0.05f;
292
-
293
- human_status.Human.transform.position = pos;
294
-
295
- if (human_status.Human.transform.position.x < -1.62f)
296
-
297
- {
298
-
299
- human_status.Dir = 2;
300
-
301
- }
302
-
303
- else if ((human_status.Atack_Counter == 7) || (human_status.Atack_Counter == 8))
304
-
305
- {
306
-
307
- human_status.Dir = 2;
308
-
309
- human_status.Atack_Counter = 0;
310
-
311
- }
312
-
313
- }
314
-
315
- else if (human_status.Dir == 2)
316
-
317
- {
318
-
319
- pos.x = pos.x + 0.05f;
320
-
321
- human_status.Human.transform.position = pos;
322
-
323
- if (human_status.Human.transform.position.x > 2.3f)
324
-
325
- {
326
-
327
- human_status.Dir = 1;
328
-
329
- }
330
-
331
- else if ((human_status.Atack_Counter == 1) || (human_status.Atack_Counter == 2))
332
-
333
- {
334
-
335
- human_status.Dir = 1;
336
-
337
- human_status.Atack_Counter = 0;
338
-
339
- }
340
-
341
- }
342
-
343
- else if (human_status.Dir == 3)
344
-
345
- {
346
-
347
- if (human_status.Human.transform.position.y < -5.0f)
348
-
349
- {
350
-
351
- human_status.Ho_x = UnityEngine.Random.Range(-1.0f, 2.3f);//最小-1.2f(-1.0f)最大(2.5f)2.3f
352
-
353
- human_status.Human.transform.position = new Vector3(human_status.Ho_x, 5.0f, 0.00f);
354
-
355
- }
356
-
357
- else if (human_status.Human.transform.position.y > 4.6f)
358
-
359
- {
360
-
361
- human_status.Human.transform.position = new Vector3(human_status.Human.transform.position.x, human_status.Human.transform.position.y + -0.05f, 0.00f);
362
-
363
- }
364
-
365
-
366
-
367
- else
368
-
369
- {
370
-
371
- human_status.Human.transform.position = new Vector3(human_status.Human.transform.position.x, human_status.Human.transform.position.y + -0.05f, 0.00f);
372
-
373
- human_status.Ho_y = UnityEngine.Random.Range(1.8f, 4.6f);//最小1.8f最大4.6f
374
-
375
- if (human_status.Ho_y <= 4.6f)
376
-
377
- {
378
-
379
- if ((human_status.Ho_y >= 1.8f) | (human_status.Ho_y <= 4.6f))
380
-
381
- {
382
-
383
- if (human_status.Human.transform.position.y > human_status.Ho_y)
384
-
385
- {
386
-
387
- human_status.Dir = UnityEngine.Random.Range(1, 4);
388
-
389
- }
390
-
391
- }
392
-
393
-
394
-
395
- }
396
-
397
- }
398
-
399
-
400
-
401
- }
402
-
403
- Chara[i] = human_status;
404
-
405
- }
406
-
407
- if (Input.GetMouseButtonDown(0) && GlobalVariables.Shoot_Flag == false)
408
-
409
- {
410
-
411
-
412
-
413
- //マウス位置座標をVector3で取得
414
-
415
- pos = Input.mousePosition;
416
-
417
- // マウス位置座標をスクリーン座標からワールド座標に変換する
418
-
419
- WorldPointPos = Camera.main.ScreenToWorldPoint(pos);
420
-
421
- point_x = WorldPointPos.x;
422
-
423
- point_y = WorldPointPos.y;
424
-
425
-
426
-
427
- if (point_x < -1.39f)
428
-
429
- {
430
-
431
- point_x = -1.35f;
432
-
433
-
434
-
435
- }
436
-
437
- if (point_y < -4.25f)
438
-
439
- {
440
-
441
- point_y = -4.25f;
442
-
443
-
444
-
445
- }
446
-
447
-
448
-
449
- //trumpの座標を取る。
450
-
451
- Vector3 tmp3 = GameObject.Find("Trump").transform.position;
452
-
453
- GameObject.Find("Trump").transform.position = new Vector3(tmp3.x, tmp3.y, tmp3.z);
454
-
455
-
456
-
457
- dx = tmp3.x;
458
-
459
- dy = tmp3.y;
460
-
461
- r_dx = point_x - dx;
462
-
463
- r_dy = point_y - dy;
464
-
465
- rad = Mathf.Atan2(r_dy, r_dx);
466
-
467
-
468
-
469
- Shot_obj = Instantiate(Resources.Load("Frying_pan_pref"), new Vector3(dx, dy, 0.0f), Quaternion.identity) as GameObject;
470
-
471
- Taget_obj = Instantiate(Resources.Load("taget3_pref"), new Vector3(point_x, point_y, 0.0f), Quaternion.identity) as GameObject;
472
-
473
- Shot_obj.transform.Rotate(new Vector3(0, 0, rad * 45.0f));
474
-
475
- GlobalVariables.Shoot_Flag = true;
476
-
477
- }
478
-
479
- else if(GlobalVariables.Shoot_Flag == true)
480
-
481
- {
482
-
483
- Vector3 Position = Shot_obj.transform.position;
484
-
485
- float xx = Mathf.Cos(rad);
486
-
487
- float yy = Mathf.Sin(rad);
488
-
489
- xx *= -0.1f;//idatenn
490
-
491
- yy *= -0.1f;//idatenn
492
-
493
- Position.x -= xx; //* Time.deltaTime ;
494
-
495
- Position.y -= yy; //* Time.deltaTime;
496
-
497
- Debug.Log("xx=" + xx);
498
-
499
- Debug.Log("yy=" + yy);
500
-
501
-
502
-
503
- S_x = Position.x;
504
-
505
- S_y = Position.y;
506
-
507
- Shot_obj.transform.position = new Vector3(S_x, S_y, 0.0f);
508
-
509
-
510
-
511
- // Shot_obj.transform.Rotate(new Vector3(0, 0, rote_z));
512
-
513
- // rote_z = rote_z + 30;
514
-
515
-
516
-
517
- if (rote_z == 360)
518
-
519
- {
520
-
521
- rote_z = 0;
522
-
523
- }
524
-
525
- }
526
-
527
- }
528
-
529
545
  }
530
546
 
531
547
 
532
548
 
549
+ コード
550
+
533
551
  ```
534
-
535
-
536
-
537
- "Mexicoman_pref"にBox Collider 2D設定をすると、動きがおかしくなり困ってます。

2

prefabに下記スクリプトを追加する事で動きがおかしくなる現象は、なくなったのですが、代わりにnullが返って来ておかしくなる

2018/05/05 12:41

投稿

uepon
uepon

スコア17

test CHANGED
File without changes
test CHANGED
File without changes

1

prefabにスクリプトを追加すると、nullが返ってきます。

2018/05/05 12:29

投稿

uepon
uepon

スコア17

test CHANGED
File without changes
test CHANGED
File without changes