23
21

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.

【Unity】terrainを繰り返して、無限に続く地面を作る

Last updated at Posted at 2014-06-20

Run系のゲームとかを作るにあたり、無限に続く地面を作りたくなりました。
そこで2つの地面を繰り返し表示することで、無限に地面が続くようにしてみました。

##地面の準備

まずは地面と、目印のCubeを作ります。

GameObject > Create Other > Terrain
で地面を追加します。お好みで適当にテクスチャを貼り付けて下さい。

GameObject > Create Other > Cube
で目印のCubeを追加します。
Cubeの設定は以下のようにして、Terrainの真ん中に置きました。

kobito.1403248293.177540.png

Terrainを動かすために以下のようなスクリプトをTerrainにアタッチしておきます。

MoveTerrain.cs

using UnityEngine;
using System.Collections;

public class MoveTerrain : MonoBehaviour {
	public float speed;

	// Update is called once per frame
	void Update () {
		transform.Translate (0, 0, speed);
	}
}

CubeはTerrainと連動して動くようにTerrainの配下にドラッグしておきます。
そしてそのTerrainは、再利用しやすいようにプレファブ化しておきます。

kobito.1403248446.611057.png

##地面の配置
プレファブ化したら元のTerrainは削除して、
Terrainプレファブを2つドラッグして設置します。
座標は(0, 0, 0)と(0, 0, 2000)にしてz方向に2つ並べます。

kobito.1403249501.880606.png

MainCameraの位置を(1000, 30, 0)あたりに置いて、実行してみます。

2つの地面がスクロールしてきますが、2つめが過ぎ去ったら終わっちゃいます。

##繰り返し処理の実装
1つめのTerrainが画面外に過ぎ去ったら、2つ目の奥にサッと移動させる、、、
という処理を繰り返すことで、地面が無限に繰り返されるようにします。

先ほどのスクリプトを以下のように変更します。

MoveTerrain.cs
public class MoveTerrain : MonoBehaviour {

	public float speed;
	float size = 2000;  //NOTE!

	// Update is called once per frame
	void Update () {
		transform.Translate (0, 0, speed);

		if (this.transform.position.z + size < 0) {
			Debug.Log("out of display");
			this.transform.Translate(0, 0, size * 2);
		}
	}
}

注)float size = 2000; terrainのサイズ、スクリプトで取得できる??誰か教えてください ><

実行してみると地面が無限に繰り返されるはずです。

23
21
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
23
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?