著作権:
- 次のWebサイトで始まるこの記事オリジナル:
- ブログパーク「優れた夢のメイカーズムーブメント」空間にします。https://www.cnblogs.com/raymondking123
- 優秀な夢メイカーズムーブメント公式ブログます。https://91make.top
- 優れたゲーム夢のメーカー文化講演します。https://91make.ke.qq.com
- マイクロチャンネル公衆数の「優秀な夢メイカーズムーブメント」:umaketop
- あなたは転載は自由ですが、完全な著作権表示を含める必要があります
目標:
- レンガを作成します
- ゲームの動作時に発生ブリック
- パゴダの作成と回転
- 五重塔のダイナミックな上昇
レンガを作成します
- まず、キューブCubeBrickと彼の位置リセットビット、および適切なサイズに調整レンガ、そして自分の好きな色に調整材料の名前を変更した後、我々はプリフォームにCubeBrickを保存することができますを作成します
- そして、CylinderBrickの名前を変更し、保存するために、上記の手順に従ってシリンダーを作成します。
- だから、私たちが必要な2個のレンガを得ることができます
ゲームの動作時に発生ブリック
- まずPlatformRigプリフォーム前に、当社の生産を開き、以下に示すように、ゲームオブジェクトの空のサブセットは、タワーの名前を変更し、座標をリセットして追加
し、私たちのスクリプトでタワーと呼ばれる新しいスクリプトを作成し、スクリプトをドラッグフォルダースクリプトを開き、次のコードを書くためのタワー
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tower : MonoBehaviour
{
public int height; // 砖塔的层高
public Color[] colors = new Color[2];
public GameObject brickPrefab;
// Start is called before the first frame update
void Start()
{
// 动态生成这些砖块(从砖块预制体)
for (int i = 0; i < height; i++)
{
GameObject goBrick = Instantiate(brickPrefab);
goBrick.transform.SetParent(this.transform, false);
goBrick.GetComponent<MeshRenderer>().material.color = colors[i % 2];
}
}
// Update is called once per frame
void Update()
{
}
}
や公共へのスクリプトの変数のための次のデータの定義を変更するには
パゴダの作成と回転
タワーは、私たちのスクリプトを開き、以下に示すようにコードを変更します
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tower : MonoBehaviour
{
public int height; // 砖塔的层高
public Color[] colors = new Color[2];
public GameObject brickPrefab;
// Start is called before the first frame update
void Start()
{
// 动态生成这些砖块(从砖块预制体)
for (int i = 0; i < height; i++)
{
GameObject goBrick = Instantiate(brickPrefab);
// TODO: 让每一层砖块的高度都不一样!
goBrick.transform.Translate(0, i, 0, Space.Self); // 移动一个单位
goBrick.transform.Rotate(0, 10*i, 0, Space.Self);
goBrick.transform.SetParent(this.transform, false); // 由于Level本身带缩放,所以实际放到Tower下面后,仅平移了i*0.25个单位
goBrick.GetComponent<MeshRenderer>().material.color = colors[i % 2];
}
}
// Update is called once per frame
void Update()
{
this.transform.Rotate(0, 45 * Time.deltaTime, 0);
}
}
- その後、保存して実行して私たちのゲームは、パゴダの形成及び回転を見ることができます
五重塔のダイナミックな上昇
- まず、当社のタワースクリプトを開き、次のコードを追加することです
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tower : MonoBehaviour
{
public int height; // 砖塔的层高
public Color[] colors = new Color[2];
public GameObject brickPrefab;
// Start is called before the first frame update
void Start()
{
// 动态生成这些砖块(从砖块预制体)
for (int i = 0; i < height; i++)
{
GameObject goBrick = Instantiate(brickPrefab);
// TODO: 让每一层砖块的高度都不一样!
goBrick.transform.Translate(0, i, 0, Space.Self); // 移动一个单位
goBrick.transform.Rotate(0, 10*i, 0, Space.Self);
goBrick.transform.SetParent(this.transform, false); // 由于Level本身带缩放,所以实际放到Tower下面后,仅平移了i*0.25个单位
goBrick.GetComponent<MeshRenderer>().material.color = colors[i % 2];
}
StartCoroutine(RiseUpFromUnderground());
}
IEnumerator RiseUpFromUnderground()
{
// 将塔先放置在地下
this.transform.Translate(0, -height * transform.lossyScale.y, 0, Space.Self);
// 定时往上跑一定距离
while (this.transform.position.y < 0.5 * transform.lossyScale.y)
{
this.transform.Translate(0, 5 * Time.deltaTime, 0, Space.Self);
yield return null;
}
}
// Update is called once per frame
void Update()
{
this.transform.Rotate(0, 45 * Time.deltaTime, 0);
}
}
- 保存した後の効果の下から上昇し、当社のレンガ造りの塔を参照するには、ゲームを実行します。