LoginSignup
2
1

More than 5 years have passed since last update.

オブジェクトをジグザグに動かしてみる

Posted at

はじめに

スーパーマリオ・ソニック等アクションゲームに代表されるものに、物体・地形が動くものがほぼまちがいなくある。
その中でも基本となる上下に動くものを今回作成すると同時に、2種類の方法を用いて挙動の違いを調べてみる。

方法1 Mathf.Sinメソッド

3座標のうち
・2座標を動かす:円運動
・1座標を動かす:ジグザグ

public class CubeLR : MonoBehaviour {
    float x;                       //三角関数の数値設定
    float speed = 3f;              //スピードの数値設
    float radius = 0.1f;           //半径の設定

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


       x = radius *Mathf.Sin(Time.time * speed);  //三角関数による動きの設定。

       //X座標のみ三角関数による動きの設定を反映
       transform.position = new Vector3(x+transform.position.x,transform.position.y,transform.position.z);  

    }
}

方法2 PingPongメソッド。

PingPong(Time.time(移動スピード),3(移動距離));

public class test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        transform.position = new Vector3(Mathf.PingPong(Time.time,3),transform.position.y,transform.position.z);
    }
}

結果

青Cube : PingPong
緑Cube : Mathf.Sin

movie.gif

感想

両方ともほぼ同じ動きになるが、PingPongは速度、距離の設定がとても簡単である。
わざわざMathf.Sin関数を使うメリットは今のところ感じられません。

2
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
2
1