質問編集履歴
1
改良点の追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -136,4 +136,131 @@
|
|
136
136
|
|
137
137
|
|
138
138
|
情報等足りないところや、わかりにくいところ等あれば追記します。
|
139
|
-
ぜひ回答よろしくお願いします。
|
139
|
+
ぜひ回答よろしくお願いします。
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
|
144
|
+
以下追記になります。
|
145
|
+
kaka1102さんの回答を参考にいろいろ変更してみました。
|
146
|
+
プレイヤーはエネミーの弾は管理していなく、プレイヤーとエネミーが共通して持っているスペースシップというスクリプトで弾の撃つ撃たないを管理しています。
|
147
|
+
なので、参考サイト、kaka1102さんが仮に作成したBulletGeneratorを組み合わせて、スペースシップに追加してみました。
|
148
|
+
|
149
|
+
```lang-C#
|
150
|
+
using UnityEngine;
|
151
|
+
using System.Collections;
|
152
|
+
using System.Collections.Generic;
|
153
|
+
|
154
|
+
[RequireComponent(typeof(Rigidbody2D))]
|
155
|
+
public class Spaceship : MonoBehaviour {
|
156
|
+
|
157
|
+
List<Bullet> list_Bullets = new List<Bullet>();
|
158
|
+
|
159
|
+
const int MAX_BULLETS = 20;
|
160
|
+
|
161
|
+
|
162
|
+
public float speed;
|
163
|
+
|
164
|
+
public float shotDelay;
|
165
|
+
|
166
|
+
public GameObject bullet;
|
167
|
+
|
168
|
+
public bool canShot;
|
169
|
+
|
170
|
+
public GameObject explosion;
|
171
|
+
|
172
|
+
void Start()
|
173
|
+
{
|
174
|
+
Bullet bulleta;
|
175
|
+
// 最初に一定数の弾を備蓄しておく
|
176
|
+
for (int i = 0; i < MAX_BULLETS; i++)
|
177
|
+
{
|
178
|
+
// 弾の生成
|
179
|
+
bulleta = Instantiate(bullet).GetComponent<Bullet>();
|
180
|
+
|
181
|
+
bulleta.transform.parent = this.transform;
|
182
|
+
// 発射前は非アクティブにしておく
|
183
|
+
bulleta.gameObject.SetActive(false);
|
184
|
+
// Listに追加
|
185
|
+
list_Bullets.Add(bulleta);
|
186
|
+
}
|
187
|
+
}
|
188
|
+
|
189
|
+
|
190
|
+
public void Shot(Transform origin)
|
191
|
+
{
|
192
|
+
for (int i = 0; i < list_Bullets.Count; i++)
|
193
|
+
{
|
194
|
+
{
|
195
|
+
if (!list_Bullets[i].gameObject.activeSelf)
|
196
|
+
{
|
197
|
+
list_Bullets[i].transform.position = origin.position;
|
198
|
+
list_Bullets[i].transform.rotation = origin.rotation;
|
199
|
+
list_Bullets[i].gameObject.SetActive(true);
|
200
|
+
break;
|
201
|
+
}
|
202
|
+
}
|
203
|
+
|
204
|
+
|
205
|
+
|
206
|
+
//Instantiate(bullet, origin.position, origin.rotation);
|
207
|
+
}
|
208
|
+
}
|
209
|
+
public void Move(Vector2 direction)
|
210
|
+
{
|
211
|
+
GetComponent<Rigidbody2D>().velocity = direction * speed;
|
212
|
+
}
|
213
|
+
public void Explosion()
|
214
|
+
{
|
215
|
+
Instantiate(explosion, transform.position, transform.rotation);
|
216
|
+
}
|
217
|
+
|
218
|
+
}
|
219
|
+
```
|
220
|
+
|
221
|
+

|
222
|
+
すると、最初の一発は弾を発射するんですが、次からはこのように止まってしまいました。
|
223
|
+
|
224
|
+
この状態では、エラーは出ませんが、画像にあるDestroyAreaという画面外のオブジェクトに当たりエネミーが破壊され、次のエネミーが出てきたところで
|
225
|
+
|
226
|
+
UnassignedReferenceException: The variable bullet of Spaceship has not been assigned.
|
227
|
+
You probably need to assign the bullet variable of the Spaceship script in the inspector.
|
228
|
+
UnityEngine.Object.Instantiate[GameObject] (UnityEngine.GameObject original) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:189)
|
229
|
+
|
230
|
+
という同じエラーが3つ出てきます。
|
231
|
+
このエラーをダブルクリックした先はスペースシップスクリプトの
|
232
|
+
bulleta = Instantiate(bullet).GetComponent<Bullet>();
|
233
|
+
になります。
|
234
|
+
|
235
|
+
|
236
|
+
画像中でプレイヤーか弾が出ていませんが、プレイヤーの弾はDestroyAreaにあたると、非表示になった状態で、上へ永遠に飛んで行っていました。
|
237
|
+
|
238
|
+
DestroyAreaのスクリプトはこちらです。
|
239
|
+
|
240
|
+
```lang-C#
|
241
|
+
using UnityEngine;
|
242
|
+
using System.Collections;
|
243
|
+
|
244
|
+
public class DestroyArea : MonoBehaviour {
|
245
|
+
|
246
|
+
void OnTriggerEnter2D(Collider2D c)
|
247
|
+
{
|
248
|
+
if (c.tag == "Bullet(Player)")
|
249
|
+
{
|
250
|
+
c.gameObject.SetActive(false);
|
251
|
+
}
|
252
|
+
else if (c.tag == "Bullet(Enemy)") {
|
253
|
+
c.gameObject.SetActive(false);
|
254
|
+
}
|
255
|
+
else
|
256
|
+
{
|
257
|
+
Destroy(c.gameObject);
|
258
|
+
}
|
259
|
+
}
|
260
|
+
}
|
261
|
+
|
262
|
+
```
|
263
|
+
|
264
|
+
以上kaka1102さんの回答を参考に改良した点になります。
|
265
|
+
|
266
|
+
よろしくお願いします。
|