我想使用 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);
}
}
}
}