Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/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# 当计算每个方向的空间时,当网格大小乘以1.5时,方向是错误的,为什么?_C#_Unity3d_Unity5 - Fatal编程技术网

C# 当计算每个方向的空间时,当网格大小乘以1.5时,方向是错误的,为什么?

C# 当计算每个方向的空间时,当网格大小乘以1.5时,方向是错误的,为什么?,c#,unity3d,unity5,C#,Unity3d,Unity5,如果网格为10x10或23x7,则工作正常,但当网格在立方体之间有1.5个空间时,方向有时是错误的 这是网格脚本: using System.Collections; using System.Collections.Generic; using UnityEngine; public class GridGenerator : MonoBehaviour { public GameObject gridBlock; public int gridWidth = 10;

如果网格为10x10或23x7,则工作正常,但当网格在立方体之间有1.5个空间时,方向有时是错误的

这是网格脚本:

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 void Directions()
    {
        GridGenerator gridgenerator = GetComponent<GridGenerator>();
        Vector3 playerPosition;
        playerPosition = player.localPosition;

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

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

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

        if (playerPosition.z + 1 < gridgenerator.gridHeight * 1.5f)
        {
            // can go backward
            possibleDirections[3] = "Can go forward";
        }
        else
        {
            possibleDirections[3] = "Can't go forward";
        }
    }
private void Directions()
{
GridGenerator GridGenerator=GetComponent();
矢量3播放位置;
playerPosition=player.localPosition;
如果(playerPosition.x>0)
{
//我可以向左走
可能的方向[0]=“可以向左走”;
}
其他的
{
可能的方向[0]=“不能向左”;
}
如果(playerPosition.x+10)
{
//可以倒退
可能的方向[2]=“可以倒转”;
}
其他的
{
可能的方向[2]=“不能后退”;
}
如果(播放位置z+1
possibleDirections是数组字符串类型

当网格大小为10x10且立方体之间没有空格时,这两条线:

if (playerPosition.x + 1 < gridgenerator.gridWidth * 1.5f)
if (playerPosition.z + 1 < gridgenerator.gridHeight * 1.5f)
if(playerPosition.x+1
是:

if(playerPosition.x+1
但是当我在立方体之间添加空格时,我尝试将*1.5添加到gridgenerator.gridWidth和gridgenerator.gridHeight

但它不起作用,所以我也试着:

if (playerPosition.x + 1 < gridgenerator.gridWidth * (1 + 1.5))
if (playerPosition.z + 1 < gridgenerator.gridHeight * (1 + 1.5))
if(playerPosition.x+1
1是立方体宽度,1.5是空间。但这也不好用

在屏幕截图中,玩家位于左上角,面朝上(向前) 他不能前进,但在《检查员》中写着“可以前进”,应该是“不能前进”

只有当立方体之间有空格时才会发生这种情况

这一行是错误的:

if(playerPosition.x+1

gridWidth
变量存储多维数据集的数量,而不是它们的总间距。您有10个表示移动空间的立方体,确定超出边界该值应保持不变(仍然只有10个立方体,即使它们之间有半块空间)

您需要从播放器的场景位置(
transform.position.x
)转换为棋盘空间位置(可能除以用于隔开立方体的相同乘数)

或者,“这让我的灵魂哭泣”解决方案:

if(playerPosition.x+1.5f

因为下一个立方体距离场景单位为1.5,而不是1。这让我的灵魂哭泣,因为它让你的代码充满了硬编码的
1.5f
乘法器和偏移量,而不是将这些东西保存在一个单一的、固定的、恒定的值中,存储在其他地方,并节约使用

相关的:

possibleDirections[0]=“可以向左走”

你为什么用东西?有一些称为布尔值的值是有原因的

if (playerPosition.x + 1 < gridgenerator.gridWidth)
if (playerPosition.z + 1 < gridgenerator.gridHeight)
if (playerPosition.x + 1 < gridgenerator.gridWidth * (1 + 1.5))
if (playerPosition.z + 1 < gridgenerator.gridHeight * (1 + 1.5))