0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

螺旋状にオブジェクトを生成する

Posted at

説明

Unityで螺旋状にオブジェクトを生成します。

前回 は円状にオブジェクトを生成しました。
そのコードから単に、Z軸を少しずつずらしているだけです。

奥行きの距離を表すために、Length設定値を復活させています。
また、位置決めの前に Z軸の 足し算を行っているので、
初期値は1回分足し込み分を引いています。

20180615000750.jpg

数が少ないと、わかりにくいのでオブジェクトを100個生成してみました。

20180615000753.jpg

コード

前回まで Creater って書いていた。。
Creatorでした・・・

using UnityEngine;

public class SpiralObjectCreator : MonoBehaviour {

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

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

    [SerializeField]
    private float radius = 5f; // 半径

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

    [SerializeField]
    private float length = 50f; // Z軸の長さ


    void Start () {

        var oneCycle = 2.0f * Mathf.PI; // sin の周期は 2π
        var oneLength = length / itemCount; // Z軸の1単位
        var z = transform.position.z - oneLength; // Z軸初期位置 (生成前に足しこみをしているので、一回分引いておく)

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

            var point = ((float)i / itemCount) * oneCycle; // 周期の位置 (1.0 = 100% の時 2π となる)
            var repeatPoint = point * repeat; // 繰り返し位置

            var x = Mathf.Sin(repeatPoint) * radius;
            var y = Mathf.Cos(repeatPoint) * radius;
            z += oneLength;

            var position = new Vector3(x, y, z);

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

        }
		
    }
	
}

GitHub

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?