我怎样才能找到墙的边界并将它们与玩家的 4 个方向进行比较,以找到玩家可以移动到的方向?

How can i find the walls boundaries and compare them to the 4 directions from the player to find what directions the player can move to?

我有一个立方体网格。 本例中的网格大小为 10x10 每个立方体大小为 1x1 每面墙都是 child 墙下,每面墙上都有 childs 的墙立方体。

The Walls parent GameObject 位置在 X = 29.4 Y = 0.5 Z = 10.9 每个墙的位置都在 0,0,0

创建网格后,我找到了墙,然后我得到了玩家的当前位置,然后我得到了玩家的每个方向 + 1.5f 距离。

接下来我想找出玩家可以移动的方向。向前,向左,向右,向后,因为玩家每次都重生 运行 游戏在墙的边缘一次,然后一个方向或两个方向不能移动到。

问题是如何找到不能移动到的 direction/s 和那些可以移动的?

private void Directions()
    {
        GameObject walls = GameObject.Find("Walls");
        Vector3 playerPosition = player.position;

        Vector3 rightOnePointFive = player.localPosition + player.right * 1.5f;
        Vector3 leftOnePointFive = player.localPosition - player.right * 1.5f;
        Vector3 forwardOnePointFive = player.localPosition + player.forward * 1.5f;
        Vector3 backOnePointFive = player.localPosition - player.forward * 1.5f;
    }

这就是我创建网格和墙壁的方式:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GridGenerator : MonoBehaviour
{
    public GameObject gridBlock;
    public int gridWidth = 10;
    public int gridHeight = 10;
    public GameObject[] allBlocks;

    private GameObject[] wallsParents = new GameObject[4];

    void Start()
    {
        wallsParents[0] = GameObject.Find("Top Wall");
        wallsParents[1] = GameObject.Find("Left Wall");
        wallsParents[2] = GameObject.Find("Right Wall");
        wallsParents[3] = GameObject.Find("Bottom Wall");

        GenerateGrid();
        allBlocks = GameObject.FindGameObjectsWithTag("Blocks");

        var findpath = GetComponent<PathFinder>();
        findpath.FindPath();
    }

    public void AutoGenerateGrid()
    {
        allBlocks = GameObject.FindGameObjectsWithTag("Blocks");

        for (int i = 0; i < allBlocks.Length; i++)
        {
            DestroyImmediate(allBlocks[i]);
        }

        var end = GameObject.FindGameObjectWithTag("End");
        DestroyImmediate(end);

        GenerateGrid();
        allBlocks = GameObject.FindGameObjectsWithTag("Blocks");

        var findpath = GetComponent<PathFinder>();
        findpath.FindPath();
    }

    public void GenerateGrid()
    {
        for (int x = 0; x < gridWidth; x++)
        {
            for (int z = 0; z < gridHeight; z++)
            {
                GameObject block = Instantiate(gridBlock, Vector3.zero, gridBlock.transform.rotation) as GameObject;
                block.transform.parent = transform;
                block.transform.name = "Block";
                block.transform.tag = "Blocks";
                block.transform.localPosition = new Vector3(x * 1.5f, 0, z * 1.5f);
                block.GetComponent<Renderer>().material.color = new Color(241, 255, 0, 255);

                if (x == 0)//TOP
                {
                    block.transform.parent = wallsParents[0].transform;
                    block.transform.name = "TopWall";
                    block.transform.tag = "Blocks";
                }
                else if (z == 0)//LEFT
                {
                    block.transform.parent = wallsParents[1].transform;
                    block.transform.name = "LeftWall";
                    block.transform.tag = "Blocks";
                }
                else if (z == gridHeight - 1)//RIGHT
                {
                    block.transform.parent = wallsParents[2].transform;
                    block.transform.name = "RightWall";
                    block.transform.tag = "Blocks";
                }

                else if (x == gridWidth - 1)//BOTTOM
                {
                    block.transform.parent = wallsParents[3].transform;
                    block.transform.name = "BottomWall";
                    block.transform.tag = "Blocks";
                }
            }
        }
    }
}

更新:

我试过这个方法:

private GridGenerator gridgenerator;

public void FindDirection()
{
    gridgenerator = GetComponent<GridGenerator>();
    GenerateStartEnd();
    Directions();
}

private void Directions()
{
    Vector3 playerPosition;
    playerPosition = player.localPosition;

    if (playerPosition.x > 0)
    {
        // can go left
        possibleDirections[0] = "Can go left";
    }

    if (playerPosition.x + 1 < gridgenerator.gridWidth)
    {
        // can go right
        possibleDirections[1] = "Can go right";
    }

    if (playerPosition.z > 0)
    {
        // can go forward
        possibleDirections[2] = "Can go forward";
    }

    if (playerPosition.z + 1 < gridgenerator.gridHeight)
    {
        // can go backward
        possibleDirections[3] = "Can go backward";
    }
}

但是当运行游戏时:

我唯一不能走的方向是左边,但它在检查员中说我可以向左走。并且可以前进但不能后退和右转

我搞砸了

在这种情况下,gridWidth 和 gridHeight 都是 10,但它可以是任何尺寸,例如 23x5、9x7 或 12x12

最简单的方法是使用二维数组。

有了这个,可以很容易地通过保存当前玩家位置来检查你的玩家可以去哪里。这意味着,如果你的玩家在位置 [0,5],他不能移动到左侧。

此外,您还可以自动生成字段并轻松调整其大小。

编辑: 但是,如果您真的想保留当前的解决方案,则可以循环遍历每一堵墙,并在您想向左移动时检查 x 位置是否小于您当前的玩家位置。其他方向也一样。

编辑2:

看看这张漂亮的画图,以获得更好的例子。

如果你有一个二维数组,你可以存储当前玩家位置,然后执行以下代码。

好吧,也许我还不够清楚,但是已经凌晨 4 点了,我有点累了。你要做的就是存储当前的playerPosition,它只能是整数。因此,可能性是例如:[0,0]、[3,0]、[2,3]。每当您将玩家向左移动时,您都必须更新他在网格上的位置,例如当您向左移动时 playerPosition.X--;

Vector2 playerPos = new Vector(0, 3);
int maxWidthOfGrid = 5;


private void Directions()
{
    if(playerPos.X > 0) {
        //player can go left
    }
    if(playerPos.X+1 < maxWidthOfGrid) {
        //player can go right
    }

}

我没有测试代码,所以也许你需要稍微修改一下。

底部和顶部方向相同。