質問編集履歴

2

誤字の修正

2017/05/21 12:37

投稿

shorter
shorter

スコア13

test CHANGED
File without changes
test CHANGED
@@ -322,11 +322,11 @@
322
322
 
323
323
 
324
324
 
325
- // 弾をプレイヤーと同じ位置/角度で作成
325
+ // 弾を敵オブジェクトと同じ位置/角度で作成
326
326
 
327
327
  Instantiate (muzzle, transform.position, transform.rotation);
328
328
 
329
- // 0.05秒待つ
329
+ // shotDelay秒待つ
330
330
 
331
331
  yield return new WaitForSeconds (shotDelay);
332
332
 

1

ソースコードの追加

2017/05/21 12:37

投稿

shorter
shorter

スコア13

test CHANGED
File without changes
test CHANGED
@@ -27,3 +27,415 @@
27
27
  ヒントやアドバイス、解決法の提案等、何でも構いませんので、お答え頂きたく思います。
28
28
 
29
29
  よろしくお願い致します。
30
+
31
+
32
+
33
+ ###追記
34
+
35
+ 文章のみの質問で申し訳ありませんでした。
36
+
37
+
38
+
39
+ 以下に当問題に関係があると考えているスクリプトを添付させて頂きます。
40
+
41
+ 読みにくいコードかとは思いますが、よろしくお願い致します。
42
+
43
+
44
+
45
+ ```c#
46
+
47
+ using System.Collections;
48
+
49
+ using System.Collections.Generic;
50
+
51
+ using UnityEngine;
52
+
53
+
54
+
55
+ public class PlayerParameter : MonoBehaviour {
56
+
57
+
58
+
59
+ //PlayerのRigidbody2D
60
+
61
+ private Rigidbody2D rb2d;
62
+
63
+ //Playerの速度
64
+
65
+ private float v = 0.0f;
66
+
67
+ //Playerの速度ベクトル
68
+
69
+ private Vector3 vVector;
70
+
71
+ //Playerの現在位置
72
+
73
+ private Vector3 nowPos;
74
+
75
+ //Playerの前フレームの位置
76
+
77
+ private Vector3 prevPos = Vector3.zero;
78
+
79
+ //Playerの移動ベクトル
80
+
81
+ private Vector3 moveVec = Vector3.zero;
82
+
83
+
84
+
85
+ // Use this for initialization
86
+
87
+ void Start () {
88
+
89
+ //Rigidbody2Dコンポーネントを取得
90
+
91
+ rb2d = this.GetComponent<Rigidbody2D> ();
92
+
93
+ }
94
+
95
+
96
+
97
+ // Update is called once per frame
98
+
99
+ void Update () {
100
+
101
+ //Playerの現在位置を格納
102
+
103
+ nowPos = this.transform.position;
104
+
105
+ //Playerの速度値を格納
106
+
107
+ v = rb2d.velocity.magnitude;
108
+
109
+ //Playerの速度ベクトルを格納
110
+
111
+ vVector = rb2d.velocity;
112
+
113
+ //Playerの移動ベクトルを格納
114
+
115
+ moveVec = nowPos - prevPos;
116
+
117
+ //Playerの現在位置を前フレームの位置にする
118
+
119
+ prevPos = nowPos;
120
+
121
+ }
122
+
123
+
124
+
125
+ //Playerの速度を返す関数
126
+
127
+ public float GetVelocity(){
128
+
129
+ return v;
130
+
131
+ }
132
+
133
+
134
+
135
+ //Playerの速度ベクトルを返す関数
136
+
137
+ public Vector3 GetVelocityVector(){
138
+
139
+ return vVector;
140
+
141
+ }
142
+
143
+
144
+
145
+ //Playerの移動ベクトルを返す関数
146
+
147
+ public Vector3 GetMoveVector(){
148
+
149
+ return moveVec;
150
+
151
+ }
152
+
153
+ }
154
+
155
+ ```
156
+
157
+ ```c#
158
+
159
+ using System.Collections;
160
+
161
+ using System.Collections.Generic;
162
+
163
+ using UnityEngine;
164
+
165
+
166
+
167
+ public class Enemy : MonoBehaviour {
168
+
169
+
170
+
171
+ //標的(Player)
172
+
173
+ private GameObject playerObj;
174
+
175
+ //自分(Enemy)の座標
176
+
177
+ private Vector3 mPos;
178
+
179
+ //Playerの座標
180
+
181
+ private Vector3 pPos;
182
+
183
+ //Playerの移動予測座標(偏差射撃のため)
184
+
185
+ private Vector3 forecastPos;
186
+
187
+ //Playerの速度
188
+
189
+ private float p_velocity;
190
+
191
+ //PlayerとEnemy間の距離
192
+
193
+ private float p_distance;
194
+
195
+
196
+
197
+ // Use this for initialization
198
+
199
+ void Start () {
200
+
201
+ //Playerオブジェクトを取得
202
+
203
+ playerObj = GameObject.FindWithTag ("Player");
204
+
205
+ }
206
+
207
+
208
+
209
+ // Update is called once per frame
210
+
211
+ void Update () {
212
+
213
+ //PlayerにアタッチされているPlayerParameterスクリプトを参照
214
+
215
+ PlayerParameter pp = playerObj.GetComponent<PlayerParameter> ();
216
+
217
+ //Shootingスクリプトを参照
218
+
219
+ Shooting st = GetComponent<Shooting> ();
220
+
221
+ //自分(Enemy)の座標を取得
222
+
223
+ mPos = transform.position;
224
+
225
+ //Playerの座標を取得
226
+
227
+ pPos = playerObj.transform.position;
228
+
229
+ //Playerの速度を格納
230
+
231
+ p_velocity = pp.GetVelocity();
232
+
233
+ //PlayerとEnemy間の距離を格納
234
+
235
+ //p_distance = Vector3.Distance(mPos, pPos);
236
+
237
+ //Playerの速度を送る
238
+
239
+ st.SetPlayerVelocity (p_velocity);
240
+
241
+ //Playerの予測移動位置を格納
242
+
243
+ forecastPos = pPos + pp.GetVelocityVector();
244
+
245
+ //targetを向く
246
+
247
+ transform.rotation = Quaternion.FromToRotation( Vector3.up, forecastPos);
248
+
249
+ //PlayerのY座標を取得し、自分(Enemy)の座標に反映(=Playerと平行移動)
250
+
251
+ mPos.y = pPos.y;
252
+
253
+ transform.position = mPos;
254
+
255
+ }
256
+
257
+ }
258
+
259
+ ```
260
+
261
+ ```c#
262
+
263
+ using System.Collections;
264
+
265
+ using System.Collections.Generic;
266
+
267
+ using UnityEngine;
268
+
269
+
270
+
271
+ //Rigidbody2Dコンポーネントを必須にする
272
+
273
+ [RequireComponent(typeof(Rigidbody2D))]
274
+
275
+ public class Shooting : MonoBehaviour {
276
+
277
+
278
+
279
+ //Bulletプレハブ
280
+
281
+ [SerializeField]
282
+
283
+ private GameObject muzzle;
284
+
285
+ //弾を撃つ間隔
286
+
287
+ [SerializeField]
288
+
289
+ private float shotDelay = 1f;
290
+
291
+ //弾の速度
292
+
293
+ [SerializeField]
294
+
295
+ private float bulletSpeed = 5f;
296
+
297
+ //Playerの速度
298
+
299
+ private float p_velocity = 0f;
300
+
301
+
302
+
303
+ // Update is called once per frame
304
+
305
+ void Update () {
306
+
307
+
308
+
309
+ }
310
+
311
+
312
+
313
+ // Startメソッドをコルーチンとして呼び出す
314
+
315
+ IEnumerator Start ()
316
+
317
+ {
318
+
319
+ while (true) {
320
+
321
+ //Debug.Log (GetBulletSpeed());
322
+
323
+
324
+
325
+ // 弾をプレイヤーと同じ位置/角度で作成
326
+
327
+ Instantiate (muzzle, transform.position, transform.rotation);
328
+
329
+ // 0.05秒待つ
330
+
331
+ yield return new WaitForSeconds (shotDelay);
332
+
333
+ }
334
+
335
+ }
336
+
337
+
338
+
339
+ //Enemyの速度を加算した弾の速度を返す
340
+
341
+ public float GetBulletSpeed(){
342
+
343
+ return bulletSpeed;
344
+
345
+ }
346
+
347
+
348
+
349
+ //Playerの速度をセットする関数
350
+
351
+ public void SetPlayerVelocity(float v){
352
+
353
+ p_velocity = v;
354
+
355
+ }
356
+
357
+
358
+
359
+ //Playerの速度を取得する関数
360
+
361
+ public float GetPlayerVelocity(){
362
+
363
+ return p_velocity;
364
+
365
+ }
366
+
367
+ }
368
+
369
+
370
+
371
+ ```
372
+
373
+ ```c#
374
+
375
+ using System.Collections;
376
+
377
+ using System.Collections.Generic;
378
+
379
+ using UnityEngine;
380
+
381
+
382
+
383
+ //Rigidbody2Dコンポーネントを必須にする
384
+
385
+ [RequireComponent(typeof(Rigidbody2D))]
386
+
387
+ public class Bullet : MonoBehaviour {
388
+
389
+
390
+
391
+ //敵オブジェクト
392
+
393
+ private GameObject eneObj;
394
+
395
+ //Playerオブジェクト
396
+
397
+ private GameObject playerObj;
398
+
399
+
400
+
401
+ // Use this for initialization
402
+
403
+ void Start () {
404
+
405
+ //Playerオブジェクトを取得
406
+
407
+ playerObj = GameObject.FindWithTag ("Player");
408
+
409
+ //Enemyオブジェクトを取得
410
+
411
+ eneObj = GameObject.FindWithTag ("Enemy");
412
+
413
+ //EnemyオブジェクトのShootingスクリプトを参照
414
+
415
+ Shooting st = eneObj.GetComponent<Shooting> ();
416
+
417
+ //弾を発射する
418
+
419
+ GetComponent<Rigidbody2D> ().AddForce (transform.up.normalized * st.GetBulletSpeed(),ForceMode2D.Impulse);
420
+
421
+ }
422
+
423
+
424
+
425
+ // Update is called once per frame
426
+
427
+ void Update () {
428
+
429
+ //PlayerにアタッチされているPlayerParameterスクリプトを参照
430
+
431
+ PlayerParameter pp = playerObj.GetComponent<PlayerParameter> ();
432
+
433
+ //Playerの移動ベクトルを加える
434
+
435
+ transform.position += pp.GetMoveVector ();
436
+
437
+ }
438
+
439
+ }
440
+
441
+ ```