当网格大小乘以 1.5 时计算每个方向的 space 时方向错误,为什么?

When calculating the space for each direction when the grid size is multiply by 1.5 the directions are wrong why?

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

这是网格脚本:

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";
                }
            }
        }
    }
}

在这一行中,我在立方体之间添加 spaces:

block.transform.localPosition = new Vector3(x * 1.5f, 0, z * 1.5f);

然后在另一个脚本中,我试图找到接下来可以移动到的方向。

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";
        }
    }

possibleDirections 是数组字符串类型

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

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

是:

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

但是当我在立方体之间添加 spaces 时,我试图添加到 gridgenerator.gridWidth 和 gridgenerator.gridHeight * 1.5

但是没用所以我也试了:

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

1 是立方体宽度,1.5 是 space。但这也不好用。

在屏幕截图中,玩家位于左上角,面朝上(向前) 他不能前进,但在检查员中它说 "Can go forward" 应该是 "Can't go forward"

只有当立方体之间有 space 时才会发生。

这一行是错误的:

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

您的 gridWidth 变量存储 立方体的数量 ,而不是它们的 集体间距。 您有 10 个立方体代表移动 spaces,确定越界这个值应该保持不变(它仍然只有 10 个立方体,即使它们是 spaced,它们之间有半块价值 space ).

您需要将玩家的 场景位置 (transform.position.x) 转换为棋盘 space 位置(可能除以用于 space立方体出来了)。

或者,"this makes my soul cry" 解决方案:

if (playerPosition.x + 1.5f < gridgenerator.gridWidth * 1.5f)

因为下一个立方体距离 1.5 场景单位,而不是 1。这让我的灵魂哭泣,因为它让你的代码充满了硬编码的 1.5f 乘数和偏移量,而不是将这些东西保存为一个单一的、固定的、常量的值,存储在别处并谨慎使用。

相关:

possibleDirections[0] = "Can go left";

你为什么要使用 stringly typed 东西?出于某种原因,存在称为布尔值的值...