LoginSignup
1
1

More than 1 year has passed since last update.

インスタンス時に指定の回転を加える方法

Last updated at Posted at 2021-07-30

課題点「細い玉の形状でまっすぐ玉を飛ばしたい」
スクリーンショット (33).png

解決方法「オブジェクトを回転させよう」

1.どうやったらまっすぐになるか考えてみよう
「Rotation(0,0,0)だと変な向きになっちゃう」
スクリーンショット (37).png
「Rotation(90,0,0)だとうまくいった!」
スクリーンショット (38).png

自分が作った玉のオブジェクトの形によって、回転する軸や量が変わるのでまっすぐになる向きを考えてみよう。

2.プログラムを変更しよう

変更前

instance.cs
void Shoot()
    {
        GameObject clone=Instantiate(bulletObj,transform.position,Quaternion.identity);
        clone.GetComponent<Rigidbody>().AddForce(transform.forward* bulletSpeed);
    }

注目ポイント「Instantiateの()の中の意味」
()の中にいれるものは3つ
1つ目はクローンしたいゲームオブジェクト
2つ目はクローンしたゲームオブジェクトの座標
3つ目はクローンしたゲームオブジェクトの回転

なので下のプログラムの意味は
Instantiate(bulletObj,transform.position,Quaternion.identity);
クローンする(弾ゲームオブジェクト,このプログラムを持っているゲームオブジェクトの座標,Rotation(0,0,0));
*Quaternion.identity = Rotation(0,0,0) =無回転状態
Rotation(0,0,0)だと変な向きになったので、Quaternion.identityを変えたい!
Rotation(0,0,0)をRotation(90,0,0)にすればまっすぐ弾が回転しそう...

変更後

instance.cs
void Shoot()
    {
        Quaternion rot =Quaternion.Euler(90,0,0);
        GameObject clone=Instantiate(bulletObj,transform.position,rot);
        clone.GetComponent<Rigidbody>().AddForce(transform.forward* bulletSpeed);
    }

Quaternion.Euler(90,0,0) =Rotation(90,0,0)にしてインスタンスをすると弾がまっすぐ飛びます
上の画像のようにRotation(90,0,0)で生成されて、弾が飛ぶようになりました!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1