質問編集履歴
1
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -108,4 +108,148 @@
|
|
108
108
|
}
|
109
109
|
```
|
110
110
|
|
111
|
-
Unityバージョン:Unity 2019.2.15f1 (64-bit)
|
111
|
+
Unityバージョン:Unity 2019.2.15f1 (64-bit)
|
112
|
+
|
113
|
+
追記:5/13に教えて頂いたスクリプトについて
|
114
|
+
|
115
|
+
スクリプトを教えて頂いたものに変更したところ、弾がプレイヤーの後ろに飛んで行く様になりました。
|
116
|
+
やはりプレイヤーの向きが変わっても、弾の方向は変わっていません。
|
117
|
+
足りていないと思って、私が自分で足した部分が原因でしょうか?
|
118
|
+
|
119
|
+
```ここに言語を入力
|
120
|
+
using System.Collections;
|
121
|
+
using System.Collections.Generic;
|
122
|
+
using UnityEngine;
|
123
|
+
|
124
|
+
public class Bullet : MonoBehaviour
|
125
|
+
{
|
126
|
+
|
127
|
+
// 弾オブジェクト(Inspectorでオブジェクトを指定)
|
128
|
+
[SerializeField] // Inspectorで操作できるように属性を追加します
|
129
|
+
private GameObject bullet;
|
130
|
+
|
131
|
+
// 弾オブジェクトのRigidbody2Dの入れ物
|
132
|
+
private Rigidbody2D rb2d;
|
133
|
+
// 弾オブジェクトの移動係数(速度調整用)
|
134
|
+
float bulletSpeed;
|
135
|
+
|
136
|
+
private Vector2 direction = Vector2.right;
|
137
|
+
|
138
|
+
void Start()
|
139
|
+
{
|
140
|
+
// オブジェクトのRigidbody2Dを取得
|
141
|
+
rb2d = GetComponent<Rigidbody2D>();
|
142
|
+
// 弾オブジェクトの移動係数を初期化
|
143
|
+
bulletSpeed = 10.0f;
|
144
|
+
// 出現から3秒後に弾オブジェクトを消滅させる(メモリの節約)
|
145
|
+
Destroy(gameObject, 3.0f);
|
146
|
+
}
|
147
|
+
|
148
|
+
void Update()
|
149
|
+
{
|
150
|
+
// 弾オブジェクトの移動関数
|
151
|
+
BulletMove();
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
public void SetDirection(bool isLeft)
|
158
|
+
{
|
159
|
+
// 左方向なら(-1.0, 0.0)方向に、右方向なら(1.0, 0.0)方向に進む
|
160
|
+
direction = isLeft ? Vector2.left : Vector2.right;
|
161
|
+
|
162
|
+
// スプライト画像の反転処理(画像のデフォルトが右方向の向いているもの前提)
|
163
|
+
var sprite = GetComponent<SpriteRenderer>();
|
164
|
+
if (sprite != null)
|
165
|
+
{
|
166
|
+
sprite.flipY = isLeft;
|
167
|
+
}
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
void BulletMove()
|
172
|
+
{
|
173
|
+
// Rigidbody2D に移動量を加算する
|
174
|
+
rb2d.velocity = direction * bulletSpeed;
|
175
|
+
}
|
176
|
+
|
177
|
+
// ENEMYと接触したときの関数
|
178
|
+
void OnCollisionEnter2D(Collision2D collision)
|
179
|
+
{
|
180
|
+
// ENEMYに弾が接触したら弾は消滅する
|
181
|
+
if (collision.gameObject.tag == "Enemy")
|
182
|
+
{
|
183
|
+
Destroy(gameObject);
|
184
|
+
}
|
185
|
+
|
186
|
+
if (collision.gameObject.tag == "Ground")
|
187
|
+
{
|
188
|
+
Destroy(gameObject);
|
189
|
+
}
|
190
|
+
|
191
|
+
if (collision.gameObject.tag == "MoveFloor")
|
192
|
+
{
|
193
|
+
Destroy(gameObject);
|
194
|
+
}
|
195
|
+
|
196
|
+
if (collision.gameObject.tag == "JumpStep")
|
197
|
+
{
|
198
|
+
Destroy(gameObject);
|
199
|
+
}
|
200
|
+
|
201
|
+
if (collision.gameObject.tag == "GroundPlatform")
|
202
|
+
{
|
203
|
+
Destroy(gameObject);
|
204
|
+
}
|
205
|
+
|
206
|
+
}
|
207
|
+
}
|
208
|
+
|
209
|
+
```
|
210
|
+
|
211
|
+
```ここに言語を入力
|
212
|
+
using System.Collections;
|
213
|
+
using System.Collections.Generic;
|
214
|
+
using UnityEngine;
|
215
|
+
|
216
|
+
public class PlayerBullet : MonoBehaviour
|
217
|
+
{
|
218
|
+
#pragma warning disable 0649
|
219
|
+
// InspectorでPrefab化したBulletを指定する
|
220
|
+
[SerializeField]
|
221
|
+
private GameObject bullet;
|
222
|
+
|
223
|
+
// ユーザーがどちらを向いているか
|
224
|
+
private bool isLeft = true;
|
225
|
+
|
226
|
+
void Update()
|
227
|
+
{
|
228
|
+
// 弾オブジェクトを生成して飛ばす関数を呼び出す
|
229
|
+
ShotAction();
|
230
|
+
}
|
231
|
+
|
232
|
+
void ShotAction()
|
233
|
+
{
|
234
|
+
Vector3 offset = new Vector3(1.0f, 0.0f, 0.0f);
|
235
|
+
|
236
|
+
// isLeft は仮の変数です。現在向いている方向を判定するフラグなどを用意してみてください。
|
237
|
+
// 向いている方向が反対ならx座標を反転する
|
238
|
+
if (isLeft)
|
239
|
+
{
|
240
|
+
offset.x = -offset.x;
|
241
|
+
}
|
242
|
+
|
243
|
+
if (Input.GetButtonDown("Fire1"))
|
244
|
+
{
|
245
|
+
// Bulletコンポーネントがアタッチしている前提
|
246
|
+
var bulletObj = Instantiate(bullet, transform.position + offset, transform.rotation);
|
247
|
+
bulletObj.GetComponent<Bullet>().SetDirection(isLeft);
|
248
|
+
}
|
249
|
+
}
|
250
|
+
}
|
251
|
+
|
252
|
+
```
|
253
|
+
|
254
|
+

|
255
|
+

|