LoginSignup
0
1

More than 3 years have passed since last update.

サインカーブ状にオブジェクトを生成する

Posted at

説明

Unityでサインカーブ状にオブジェクトを生成します。

20180612232450.jpg

赤線は説明用の手書きです。

sinカーブを2周期で、20個のオブジェクトを生成。
全体の距離が20で、上下の振幅を2倍大げさに見せています。

各オブジェクトの配置間隔は 距離 / オブジェクト数 で求めています。

createObjectに設定したGameObjectを繰り返して貼り付けます。

20180612232453.jpg

パラメータの詳細はコードのコメントを参照してください。

サンプルコード


using UnityEngine;

public class ObjectCreaterCurve : MonoBehaviour {

    [SerializeField]
    private GameObject createObject; // 生成するオブジェクト

    [SerializeField]
    private int itemCount = 20; // 生成するオブジェクトの数

    [SerializeField]
    private float length = 20f; // アイテムの広がる距離

    [SerializeField]
    private float expantion = 2f; // 高さ変動の拡大値

    [SerializeField]
    private float repeat = 2f; // 何周期するか


    void Start () {

        var unit = length / itemCount; // オブジェクトの配置間隔
        var oneCycle = 2.0f * Mathf.PI; // sin の周期は 2π

        for (var i = 0; i < itemCount; ++i)
        {

            var ratio = (float)i / itemCount; // 周期の位置 (1.0 = 100% の時 2π となる)

            var x = i * unit;
            var y = Mathf.Sin(ratio * oneCycle * repeat) * expantion;

            var position = new Vector3(x, y);

            Instantiate(
                createObject, 
                position, 
                Quaternion.identity, 
                transform
            );

        }


    }
}

プロジェクトファイル

gitHubにプロジェクト毎アップロードしています。
https://github.com/becky3/unity-test-SinCurve

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