Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我如何找到墙的边界,并将其与玩家的4个方向进行比较,以找到玩家可以移动的方向?_C#_Unity3d_Unity5 - Fatal编程技术网

C# 我如何找到墙的边界,并将其与玩家的4个方向进行比较,以找到玩家可以移动的方向?

C# 我如何找到墙的边界,并将其与玩家的4个方向进行比较,以找到玩家可以移动的方向?,c#,unity3d,unity5,C#,Unity3d,Unity5,我有一个立方体网格。 本例中的网格大小为10x10 每个立方体大小为1x1 每面墙都是墙下的子墙,每面墙上都有作为子墙的墙立方体 父游戏对象的位置是X=29.4Y=0.5Z=10.9 每个墙的位置都是0,0,0 创建网格后,我找到墙,然后得到玩家当前位置,然后从玩家+1.5f距离得到每个方向 接下来我想找出玩家可以移动的方向。向前、向左、向右、向后,因为玩家每次在一道墙边上运行游戏时都是“繁殖”,所以一个方向或两个方向不能移动 问题是如何找到无法移动的方向和可以移动的方向 private vo

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

父游戏对象的位置是X=29.4Y=0.5Z=10.9 每个墙的位置都是0,0,0

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

接下来我想找出玩家可以移动的方向。向前、向左、向右、向后,因为玩家每次在一道墙边上运行游戏时都是“繁殖”,所以一个方向或两个方向不能移动

问题是如何找到无法移动的方向和可以移动的方向

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";
                }
            }
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类GridGenerator:单行为
{
公共游戏对象网格块;
公共int gridWidth=10;
公共网格高度=10;
公共游戏对象[]所有区块;
私有游戏对象[]wallsParents=新游戏对象[4];
void Start()
{
wallsParents[0]=GameObject.Find(“顶墙”);
wallsParents[1]=GameObject.Find(“左墙”);
wallsParents[2]=GameObject.Find(“右墙”);
wallsParents[3]=GameObject.Find(“底墙”);
GenerateGrid();
allBlocks=GameObject.FindGameObjectsWithTag(“块”);
var findpath=GetComponent();
findpath.findpath();
}
public void AutoGenerateGrid()
{
allBlocks=GameObject.FindGameObjectsWithTag(“块”);
for(int i=0;i
更新:

我试过这个方法:

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";
    }
}
专用网格生成器;
public void FindDirection()
{
gridgenerator=GetComponent();
GenerateStartEnd();
方向();
}
私人指示()
{
矢量3播放位置;
playerPosition=player.localPosition;
如果(playerPosition.x>0)
{
//我可以向左走
可能的方向[0]=“可以向左走”;
}
if(playerPosition.x+10)
{
//可以前进
可能的方向[2]=“可以继续”;
}
if(播放位置z+1
但在运行游戏时:

我唯一不能走的方向是左边,但在检查员那里我可以走左边。可以向前走,但不能向后向右走

我把一切都搞砸了


在这种情况下,gridWidth和gridHeight都是10,但可以是23x5、9x7或12x12这样的任意大小。最简单的方法是使用二维数组

通过保存当前玩家位置,可以很容易地检查玩家可以去哪里。这意味着,如果您的玩家处于[0,5]位置,他将无法移动到左侧

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

编辑: 然而,如果你真的想保留你当前的解决方案,你可以在你想向左移动时,在每面墙中循环,检查x位置是否小于你当前的玩家位置。其他方向也一样

编辑2:

请看这幅漂亮的图画,看一个更好的例子

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

好吧,也许我不够清楚,但已经是凌晨4点了,我有点累了。您需要做的是存储当前的playerPosition,它只能是整数。因此,可能性将是一个例子
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
    }

}