0

Unity と C# を使用して Terraria や Starbound のゲームのようにタイルからマップを生成したいと考えています。

using UnityEngine;
using System.Collections;

public class MapGeneration : MonoBehaviour {
public int mapWidth, mapHeight, spreadDegree;
GameObject[,] blocks;
public GameObject baseBlock;

public int maxHeightAllowed;
public int nullDispersion;
int dispersionZone;

void Start() {
    GenerateMap();
}

void GenerateMap() {
    dispersionZone = maxHeightAllowed - nullDispersion;
    blocks = new GameObject[mapWidth, mapHeight];
    for(int x = 0; x < blocks.GetLength(0); x++) {
            for(int y = nullDispersion; y <= maxHeightAllowed; y++) {
                int heightDiff = y - nullDispersion;
                float dispersionModifier = Mathf.Sin( (heightDiff / dispersionZone) * 90);
                float random = Random.Range(0, 1);

                if(random > dispersionModifier) {
                    blocks[x,y] = (GameObject)Instantiate(baseBlock, new Vector2(x, y), Quaternion.identity);
                } else if (y < blocks.GetLength(1) && blocks[x,y+1] != null) {
                    blocks[x,y] = (GameObject)Instantiate(baseBlock, new Vector2(x, y), Quaternion.identity);
                }
            }

            for(int y = 0; y < nullDispersion; y++) {
                blocks[x,y] = (GameObject)Instantiate(baseBlock, new Vector2(x, y), Quaternion.identity);
            }
    }
}
}
4

1 に答える 1

1
(heightDiff / dispersionZone)

これが整数除算です。(heightDiff / (float)dispersionZone)代わりに使用してください。

* 90

Mathf.Sin度ではなく、ラジアンで機能します。使用する* Math.PI/2

            if(random > dispersionModifier) {
                blocks[x,y] = (GameObject)Instantiate(baseBlock, new Vector2(x, y), Quaternion.identity);
            } else if (y < blocks.GetLength(1) && blocks[x,y+1] != null) {
                blocks[x,y] = (GameObject)Instantiate(baseBlock, new Vector2(x, y), Quaternion.identity);
            }

同じタイプのブロックを作成しているときに、なぜ if を使用しているのですか? if と else の本体に何らかの違いがあってはいけませんか?

于 2014-10-29T20:20:40.457 に答える