LoginSignup
6
6

More than 3 years have passed since last update.

GameObjectを円運動させてみた2

Last updated at Posted at 2019-04-22

1. 2を作ろうとした経緯

Unityの勉強しているときにRotateAroundが出てきて、以前の投稿とは別の方法で円運動させていることから、復習もかねて作りました。2種類の方法を使い違いを調べます。

GameObjectを円運動させてみた
https://qiita.com/uroshinse/items/b167a411ce2168e0c5a0

2.RotateAround

public class Circle1 : MonoBehaviour {

    //円運動の中心となるGameObject
    [SerializeField] GameObject center;
    //円運動の速度
    float speed = 20;

    // Update is called once per frame
    void Update () {

        //RotateAround(円運動の中心,進行方向,速度)
        transform.RotateAround(center.transform.position, 
        transform.forward, speed * Time.deltaTime);

    }
}

メソッドで中心の定義ができる。スクリプトのアタッチは円運動させたいGameObject。

3.MovePosition

public class Circle : MonoBehaviour {

    //円運動させたい物体。Rigidbodyをコンポーネントさせておく
    [SerializeField] Rigidbody rbody; 
    //速度を設定
    private float speed =1.0f;
    //半径を設定
    private float radius =2.0f;

    //float型を定義しておく
    float movex;
    float movez;

    // Update is called once per frame
    void Update () {
        //movex(Sin波)・moveZ座標(Cos波)の指定をしておく。わからないときは三角関数を調べる。
        movex = radius * Mathf.Sin(Time.time * speed);
        movez = radius * Mathf.Cos(Time.time * speed);

        //Rigidbodyのオブジェクトを移動。
        rbody.MovePosition(new Vector3(movex, transform.position.y, movez));


    }
}

Vector3のY座標で中心を定義する。スクリプトのアタッチは中心(GameObject)。

4.結果

赤Cube : MovePosition
青Cube : RotateAround

movie (2).gif

赤Cube : シリンダーを中心にRotateせずに周回している。
青Cube : シリンダーの方向を向いて周回している。

5.まとめ

同じ周回でもメソッドによってはGameObjectのRotationに影響を与えることが分かった。
これら2つの使えるケースで考えられるのは

MovePosition : ゲーム全般
RotateAround : オブジェクト周りを見る

「GameObjectを円運動させてみた」も円運動はしているが、中心がわかりにくいのでオブジェクトの設置に時間がかかる。今後はこちらを使っていきます。

参考サイト

Unity3DCG
https://uni.gas.mixh.jp/unity/circle.html

 

6
6
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
6
6