前回はPaddleが動かせるようになりました.今回は回転するCylinderを一定間隔で生成します.
まず,Hierarchyビューで[Create]→[3D Object]→[Cylinder]とします.Cylinderが生成されます.Cylinderは遠くから飛来するようにしたいのでこのままでは位置があまり宜しくありません.また派手な演出にしたいので大きさも大きくしておきましょう.Inspectorビューで次のように入力します: これでCylinderができました.
次にこのシリンダーにアタッチするスクリプトを作りましょう.ProjectビューのAssetsフォルダ内を右クリックして[Create]→[C# Script]を選択し,できたファイルをCylinderControllerにリネームします.次にCylinderControllerをダブルクリックしてエディタで次のように入力します:
using UnityEngine; public class CylinderController : MonoBehaviour { float speed; float rotx; float roty; float rotz; float rndx; float rndy; float rndz; // Start is called before the first frame update void Start() { speed = Random.Range(20f, 50f) * Time.deltaTime; rotx = 0; roty = 0; rotz = 0; rndx = Random.Range(-500f, 500f) * Time.deltaTime; rndy = Random.Range(-500f, 500f) * Time.deltaTime; rndz = Random.Range(-500f, 500f) * Time.deltaTime; } // Update is called once per frame void Update() { rotx += rndx; roty += rndy; rotz += rndz; transform.rotation = Quaternion.Euler(rotx,roty,rotz); transform.Translate(0f, 0f, -speed, Space.World); if (transform.position.z < -25) { Destroy(gameObject); } } }
このスクリプトはrotx,roty,rotzがそれぞれの軸の回転角を表しています.ただし厳密には回転角は3つのパラメータでは表せず,4つ必要になります.このためUnityでは回転角を表すために4元数(Quaternion)と呼ばれる複素数をさらに拡張したものを内部で使用しているのですが,この連載のレベルを大きく逸脱するのでここでは説明しません.代わりに回転角はEuler角を使っても表せますので,Quaternion.Euler(rotx,roty,rotz)によって回転角を与え,それがrotx += rndxのようにそれぞれの軸のrndだけUpdateのたびに回転角が追加されるようになってます.また,speedも乱数で与えられていますので飛来する速さは毎回変わることになります.そしてPaddleを通り過ぎて画面外に出るとDestroy(gameObject)でCylinderを消滅させています.処理を軽くするためです.
さて,CylinderControllerスクリプトができたのでこれをCylinderにアタッチします.HierarchyビューでCylinderを選択した状態でProjectビューのAssetsフォルダにあるCylinderControllerをドラッグしてCylinderにドロップします.これでCylinderにCylinderControllerがアタッチされました.Playボタンを押すと中央に遠方から回転するCylinderが飛来してくるはずです:
さて,Cylinderは1つだけじゃなくていろいろな場所から一定間隔で複数飛んでくるようにしたいのでした.こういう時に力を発揮するのがPrefabです.HierarchyビューからCylinderを選択しProjectビューのAssetsフォルダにドロップします.すると,CylinderのPrefabができますのでCylinderPrefabにリネームしておきましょう:
Cylinderはもっぱらこれから作成するCylinderGeneratorで生成したいのでHierarchyビューからCylinderはdelateしておきます.
沢山いろいろな場所から飛来してくるCylinderを作るために,空のGamrObjectを用意し,そこにこれから作るCylinderGeneratorをアタッチしましょう.まずHierarchyビューで[Create]→[Create Empty]を選択するとHierarchyビューに空のGameObjectができますので名前をCylinderFactoryとしましょう: 次にProjectビューのAssetsフォルダ内で[Create]→[C# Script]を選択し,できたファイルをCylinderGeneratorにリネームします.それからCylinderGeneratorをダブルクリックしてスクリプトに以下を入力してください:
using UnityEngine; public class CylinderGenerator : MonoBehaviour { public GameObject CylinderPrefab; // Start is called before the first frame update void Start() { InvokeRepeating("GenerateCylinder", 0f, 4f); } // Update is called once per frame void GenerateCylinder() { Instantiate(CylinderPrefab , new Vector3(Random.Range(-20f,20f), Random.Range(2,15f),300f),Quaternion .identity); } }
このコードはInvokeRepeatingで0秒目から4秒間隔でGenerateCylinder()メソッドを実行し, GenerateCylinder()メソッドはCylinderPrafabを回転なしでz軸300の地点のx軸が-20から20,y軸が2から15の間で生成します.なお,回転が0で良いのはCylinderPrefab自体がCylinderControllerによって回転するように書かれているので,生成するときには別に回転させる必要がないからです.またy座標を2からにしたのはCylinderが地面にめり込まないようにするためです.さて,スクリプトができたら早速CylinderFactoryにアタッチしましょう.
HierarchyビューでCylinderFactoryを選択しProjectビューのAssetsフォルダ―にあるCylinderGeneratorをドラッグアンドドロップします.次にCylinderFactoryのInspectorのCylinderGenerator(Script)の欄のCylinder PrefabにAssetsフォルダ内のCylinderPrefabをドロップします:
ここで注意していただきたいのはCylinderFactoryのTransfomは今回ゲームの動作に全く影響を与えないという点です.CylinderFactorはCylinderGeneratorを実行するためだけのGameObjectなので外見という実態を持たず,そのためそこでの位置や回転やスケールといったTransformは意味がないからです.
さて,ここまで来たら早速Playをクリックして動作を確認してみましょう.問題なければ次の動画のように遠方から回転するCylinderが飛来してくるはずです:
今回はここまでです.次回はCylinderとPaddleの当たり判定をします.