Unity3d Unity如何让玩家远离墙壁

Unity3d Unity如何让玩家远离墙壁,unity3d,position,procedural-programming,Unity3d,Position,Procedural Programming,所以我把程序生成的贴图放在一起,当我点击它时,贴图会随机变化。它所做的是在平面中循环,并根据与randomfillpercent相比正方形的位置随机生成网格。如果地图坐标标记为1,则它是一堵墙;如果地图坐标标记为0,则它是一块空地。我遇到了一个问题,如果我点击地图,玩家的球体就会出现在随机生成的墙内。我想这样做,如果玩家的位置等于一个地图坐标,那就是一堵墙,然后沿着地图向下移动,直到它到达开放空间。不幸的是,我一直得到空引用错误。我希望任何人都能给我一些想法,我将不胜感激。这是我的变量和随机填充

所以我把程序生成的贴图放在一起,当我点击它时,贴图会随机变化。它所做的是在平面中循环,并根据与randomfillpercent相比正方形的位置随机生成网格。如果地图坐标标记为1,则它是一堵墙;如果地图坐标标记为0,则它是一块空地。我遇到了一个问题,如果我点击地图,玩家的球体就会出现在随机生成的墙内。我想这样做,如果玩家的位置等于一个地图坐标,那就是一堵墙,然后沿着地图向下移动,直到它到达开放空间。不幸的是,我一直得到空引用错误。我希望任何人都能给我一些想法,我将不胜感激。这是我的变量和随机填充映射函数。我没有显示全部代码。如果你有什么需要看的,请告诉我。多谢各位

public class MapGeneratorCave : MonoBehaviour {

public int width;
public int height;

public string seed;
public bool useRandomSeed;
public GameObject player;
int[,] playerPosition;

[Range(0, 100)]
public int randomFillPercent;

int[,] map;

void Start()
{
    GenerateMap();
}

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        GenerateMap();
    }
}

void GenerateMap()
{
    map = new int[width, height];
    RandomFillMap();

    for (int i = 0; i < 5; i++)
    {
        SmoothMap();
    }

    ProcessMap();

    int borderSize = 1;
    int[,] borderedMap = new int[width + borderSize * 2, height + borderSize * 2];

    for (int x = 0; x < borderedMap.GetLength(0); x++)
    {
        for (int y = 0; y < borderedMap.GetLength(1); y++)
        {
            if (x >= borderSize && x < width + borderSize && y >= borderSize && y < height + borderSize)
            {
                borderedMap[x, y] = map[x - borderSize, y - borderSize];
            }
            else
            {
                borderedMap[x, y] = 1;
            }
        }
    }

    MeshGenerator meshGen = GetComponent<MeshGenerator>();
    meshGen.GenerateMesh(borderedMap, 1);
}



 void RandomFillMap()
{
    int playerX = (int)player.transform.position.x;
    int playerY = (int)player.transform.position.y;

    if (useRandomSeed)
    {
        seed = Time.time.ToString();
    }

    System.Random pseudoRandom = new System.Random(seed.GetHashCode());

    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < height; y++)
        {
            if (x == 0 || x == width - 1 || y == 0 || y == height - 1)
            {
                map[x, y] = 1;
            }
            else
            {
                map[x, y] = (pseudoRandom.Next(0, 100) < randomFillPercent) ? 1 : 0;
            }
            if (playerPosition[playerX, playerY] == map[x, y] && map[x, y] == 1) 
            {
                playerPosition[playerX, playerY] = map[x, y - 1];
            }
        }
    }
}
公共类MapGeneratorSave:MonoBehavior{
公共整数宽度;
公众内部高度;
公共串种子;
公共bool用户和omseed;
公共游戏对象玩家;
int[,]播放位置;
[范围(0,100)]
公共投资占总投资的百分比;
int[,]图;
void Start()
{
生成映射();
}
无效更新()
{
if(Input.GetMouseButtonDown(0))
{
生成映射();
}
}
void GenerateMap()
{
地图=新整数[宽度、高度];
随机填充图();
对于(int i=0;i<5;i++)
{
SmoothMap();
}
ProcessMap();
int borderSize=1;
int[,]borderedMap=新int[宽度+边框大小*2,高度+边框大小*2];
对于(int x=0;x=borderSize&&x=borderSize&&y
由于playerPosition[playerX,playerY]不存在,您可能会得到空引用

应该使用向量2来代替多维数组(int[,])

然后你会这样做:

for (int x = 0; x < width; x++)
{
    for (int y = 0; y < height; y++)
    {
        if (x == 0 || x == width - 1 || y == 0 || y == height - 1)
        {
            map[x, y] = 1;
        }
        else
        {
            map[x, y] = (pseudoRandom.Next(0, 100) < randomFillPercent) ? 1 : 0;
        }
    }
}

while(map[playerPosition.x, playerPosition.y] == 1){  
    playerPosition.y --; 
    // make sure you aren't out of bounds and such
}
for(int x=0;x
这似乎有道理,但当我单击随机生成时,仍然存在玩家在网格内的情况,即使在网格外,也有看不见的墙,就像上一个迷宫中的墙从未真正离开过。这听起来与您在此处提出的问题完全不同。重新生成时r贴图确保您正在销毁组成旧贴图的所有旧游戏对象(网格、碰撞器等)。