新版寻路组件的使用 NavMeshSurface

        自己在做一个demo项目,需要让怪物能自动寻路,所以我决定使用新版的自动寻路,因为它可以动态烘焙(我太喜欢这功能了)。下面是官方文档网址:

导航网格表面 (NavMesh Surface) - Unity 手册

你还可以看其它的网站来多熟悉一下相关的组件功能和参数。

        经过我的使用,发觉有NavMeshSurface和NavMeshModifier这两组件就够了。我的项目是通过读取csv文件的内容来自动生成每一关的怪物、障碍物、道具等,因为每一关的障碍物位置不同,所以需要动态生成导航网格。

        首先,我的项目中有两种地图模型,不同的关卡使用其中之一,如下图:

我给两种地图都加了地面和四周的墙,都是cube碰撞器,tag设置为wall。

然后两种地图预设都加上脚本NavMeshSurface,关键参数如下:

同时所有的墙物体添加NavMeshModifier组件,参数如下,当做寻路网格的不可行走区域。我们在场景中新创建的物体如果也要当做障碍物那么也这么设置。

准备好了接下来就是测试,场景中加一个cube一个sphere,cube要作为障碍物来使用,sphere上添加一个测试脚本,代码中的destPos就在cube后面的小红圈那位置,我就是希望我按下A键的时候球能绕过cube到达目的地。

 经过测试,小球都顺利到达了目的地。

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用Java实现棋盘寻路的示例代码: ``` import java.util.*; public class ChessBoard { private int[][] board; // 棋盘 private int startX, startY; // 起点坐标 private int endX, endY; // 终点坐标 private int rows, cols; // 棋盘的行数和列数 // 构造函数,初始化棋盘和起点、终点坐标 public ChessBoard(int[][] board, int startX, int startY, int endX, int endY) { this.board = board; this.startX = startX; this.startY = startY; this.endX = endX; this.endY = endY; this.rows = board.length; this.cols = board[0].length; } // 判断一个位置是否在棋盘内 private boolean isValid(int x, int y) { return x >= 0 && x < rows && y >= 0 && y < cols; } // 获取一个位置的邻居节点 private List<int[]> getNeighbors(int x, int y) { List<int[]> neighbors = new ArrayList<>(); int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 上下左右四个方向 for (int[] dir : directions) { int nx = x + dir[0]; int ny = y + dir[1]; if (isValid(nx, ny) && board[nx][ny] == 0) { neighbors.add(new int[]{nx, ny}); } } return neighbors; } // 使用BFS算法寻找最短路径 public List<int[]> findShortestPath() { List<int[]> result = new ArrayList<>(); int[][] visited = new int[rows][cols]; // 记录每个位置是否已经访问过 int[][] parent = new int[rows][cols]; // 记录每个位置的父节点 Queue<int[]> queue = new LinkedList<>(); queue.offer(new int[]{startX, startY}); visited[startX][startY] = 1; while (!queue.isEmpty()) { int[] curr = queue.poll(); int x = curr[0]; int y = curr[1]; if (x == endX && y == endY) { // 找到了终点,回溯找到最短路径 int[] node = curr; while (node != null) { result.add(node); node = parent[node[0]][node[1]]; } Collections.reverse(result); break; } List<int[]> neighbors = getNeighbors(x, y); for (int[] neighbor : neighbors) { int nx = neighbor[0]; int ny = neighbor[1]; if (visited[nx][ny] == 0) { queue.offer(neighbor); visited[nx][ny] = 1; parent[nx][ny] = curr; } } } return result; } // 测试代码 public static void main(String[] args) { int[][] board = { {0, 0, 0, 0}, {0, 1, 0, 1}, {0, 0, 0, 0}, {0, 0, 1, 0} }; int startX = 0, startY = 0, endX = 3, endY = 3; ChessBoard chessBoard = new ChessBoard(board, startX, startY, endX, endY); List<int[]> path = chessBoard.findShortestPath(); if (path.isEmpty()) { System.out.println("No path found!"); } else { System.out.println("Shortest path is:"); for (int[] node : path) { System.out.println("(" + node[0] + ", " + node[1] + ")"); } } } } ``` 这段代码实现了一个ChessBoard类,其中包含了一个二维数组board表示棋盘,以及起点和终点的坐标。它使用BFS算法寻找最短路径,通过visited数组记录每个位置是否已经访问过,通过parent数组记录每个位置的父节点,以便最后回溯找到最短路径。在测试代码中,我们使用一个4x4的棋盘进行测试,起点为(0,0),终点为(3,3)。运行结果如下: ``` Shortest path is: (0, 0) (1, 0) (2, 0) (3, 0) (3, 1) (3, 2) (3, 3) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值