弾を撃つプログラム
Y座標の移動値を渡すことで斜めにも打てるようにした。
BulletInst(0);
BulletInst(1);
BulletInst(-1);
Rigidbody2Dではtransform.forwardはZ値が無いため使用できないらしい。
void BulletInst(float y) { //作成 GameObject obj = Instantiate(bullet, transform.position + new Vector3(0f, 0.25f, 0f), transform.rotation); //rigidbody2Dコンポーネントを取得 Rigidbody2D rb = obj.GetComponent<Rigidbody2D>(); //画像の向きをユニティちゃんに合わせる Vector2 temp = transform.localScale; temp.x = transform.localScale.x; obj.transform.localScale = temp; //ユニティちゃんの向いている向きに弾を飛ばす float spd = speed * transform.localScale.x + rigidbody2D.velocity.x; rb.velocity = new Vector2( spd, rigidbody2D.velocity.y + y); //5秒後に消滅 Destroy(obj, 5); }