【问题标题】:move along a grid in Unity在 Unity 中沿网格移动
【发布时间】:2018-01-21 11:28:47
【问题描述】:

我可以为我的播放器设置四个不同的移动方向

  • Vector2.up => (0,1)
  • Vector2.down => (0,-1)
  • Vector2.left => (-1,0)
  • Vector2.right => (1,0)

我有一个包含Cell 对象的二维数组

public class Cell
{
    public Cell(GameObject cellObject, bool isObstacle)
    {
        CellObject = cellObject;
        IsObstacle = isObstacle;
    }

    public GameObject CellObject { get; set; }

    public bool IsObstacle { get; set; }
}

我的数组是根据地图的大小来初始化的。

private const int MAP_SIZE = 10;
private Cell[,] mapCells = new Cell[MAP_SIZE, MAP_SIZE];

我使用两个循环填充这个数组,这将给我 100 个单元格。

for (int x = 0; x < MAP_SIZE; x++)
{
    for (int y = 0; y < MAP_SIZE; y++)
    {
        Vector3 newCellPosition = new Vector3(x, 0, y);
        GameObject newCellObject = Instantiate(cell, newCellPosition, cell.transform.rotation);

        bool isObstacle = false; // TEST

        Cell newCell = new Cell(newCellObject, isObstacle);
        mapCells[x, y] = newCell;
    }
}

移动玩家时,我想返回他必须移动到的Cell。 moveDirection 参数将设置要搜索的行和列。

如果有障碍物,玩家应该移动到这个障碍物并停下来。

public Cell GetTargetCell(Vector2 movementDirection)
{
    Cell targetCell = null;

    // get the path

    // get the closest obstacle

    return targetCell;
}

有没有一种优雅的方法可以通过二维方向计算正确的目标单元格?

【问题讨论】:

    标签: c# arrays unity3d


    【解决方案1】:

    我认为最优雅的方法是使用两个单独的 for 循环和 ?: operator

    //returns cell
    Cell GetTargetCell(Vector2 dir)
    {
        if(dir.y == 0) //if we're going horizontal
        {
            //HorizontalMovement
            for(int i = 0; i < ((dir.x==1)?mapSize-PLAYER_X:PLAYER_X); i++)
            {
                if(mapCells[(int)dir.x*i,PLAYER_Y].IsObstacle()) //if we encounter an obstacle
                    return mapCells[(int)dir.x*i,PLAYER_Y]; //return cell that's an obstacle
            }
    
            //if we didn't encounter an obstacle
            return mapCells[(dir.x == 1)?mapSize:0,PLAYER_Y]; //return the cell
        }
        else if(dir.x == 0)
        {
            //VerticalMovement
            //copy paste the previous for loop and edit the parameters, I'm too lazy :P
        }
        else
        {
            //NoMovement
            Debug.Log("Please enter a valid Direction");
            return mapCells[0,0];
        }
    
    
    }
    

    将 PLAYER_X 和 PLAYER_Y 值替换为玩家当前所在单元格的 x 和 y 值。我没有检查代码是否包含任何错误,但我认为它应该可以工作。

    【讨论】:

    • 您的代码让我感到困惑,因此我尝试将其转换为“更简单”的代码,但两个代码都会引发错误。使用我的示例让我在向上或向右移动时出现超出范围的异常hastebin.com/ihanocihos.cs
    • 我假设你只有在玩家没有找到障碍物时才会出现超出范围的异常。您可以通过更改 if (moveRight) { cellIndexHorizo​​ntal = MAP_SIZE; 来解决此问题} to if (moveRight) { cellIndexHorizo​​ntal = MAP_SIZE-1;并为垂直做同样的事情。由于数组从零开始计数,因此发生错误
    • 是的,我尝试过这种方式,但随后出现了其他超出范围的问题。那里出了点问题
    • 奇怪,当我阅读您的代码时,我不知道在哪些情况下它会给出超出范围的异常。您能否简要描述一下您所做的工作并附上错误消息?
    • 好的,我找到了错误发生的原因。我突出显示了代码的一部分,我更改以修复错误。 pastebin.com/W8B37EFS 。我没有修复向下的错误(但它与向左时发生的错误类型相同)。调试的几个技巧: 如果你在统一控制台中双击错误消息,它将打开 Visual Studio 并选择发生错误的代码行。您可以使用 Debug.Log() 写入控制台。例如调试日志(varA);将 varA 的值输出到控制台。
    猜你喜欢
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多