- Unity 2017 Game AI Programming(Third Edition)
- Ray Barrera Aung Sithu Kyaw Thet Naing Swe
- 534字
- 2021-07-02 19:14:12
Using A* Pathfinding
There are many games in which you can find monsters or enemies that follow the player, or go to a particular point while avoiding obstacles. For example, let's take a typical RTS game. You can select a group of units and click on a location you want them to move to, or click on the enemy units to attack them. Your units then need to find a way to reach the goal without colliding with the obstacles or avoid them as intelligently as possible. The enemy units also need to be able to do the same. Obstacles could be different for different units, terrain, or other in-game entities. For example, an air force unit might be able to pass over a mountain, while the ground or artillery units need to find a way around it. A* (pronounced "A star") is a pathfinding algorithm that is widely used in games because of its performance and accuracy. Let's take a look at an example to see how it works. Let's say we want our unit to move from point A to point B, but there's a wall in the way and it can't go straight towards the target. So, it needs to find a way to get to point B while avoiding the wall. The following figure illustrates this scenario:
In order to find the path from point A to point B, we need to know more about the map, such as the position of the obstacles. To do this, we can split our whole map into small tiles, representing the whole map in a grid format. The tiles can also be other shapes such as hexagons and triangles. Representing the whole map in a grid makes the search area more simplified, and this is an important step in pathfinding. We can now reference our map in a small 2D array:
Once our map is represented by a set of tiles, we can start searching for the best path to reach the target by calculating the movement score of each tile adjacent to the starting tile, which is a tile on the map not occupied by an obstacle, and then choosing the tile with the lowest cost. We'll pe into the specifics of how we assign scores and traverse the grid in Chapter 3, Finding Your Way, but this is the concept of A* Pathfinding in a nutshell:
A* Pathfinding calculates the cost to move across the tiles
A* is an important pattern to know when it comes to pathfinding, but Unity also gives us a couple of features right out of the box, such as automatic Navigation Mesh generation and the NavMesh agent, which we'll explore in the next section and then in more detail in Chapter 3, Finding Your Way. These features make implementing pathfinding in your games a walk in the park (no pun intended). Whether you choose to implement your own A* solution or simply go with Unity's built-in NavMesh feature will depend on your project's needs. Each option has its own pros and cons, but ultimately, knowing about both will allow you to make the best possible choice. With that said, let's have a quick look at NavMesh.
- AngularJS Web Application Development Blueprints
- R語(yǔ)言游戲數(shù)據(jù)分析與挖掘
- R語(yǔ)言數(shù)據(jù)可視化實(shí)戰(zhàn)
- Mastering Scientific Computing with R
- SAP BusinessObjects Dashboards 4.1 Cookbook
- Active Directory with PowerShell
- Java Web開(kāi)發(fā)詳解
- Unity 3D腳本編程:使用C#語(yǔ)言開(kāi)發(fā)跨平臺(tái)游戲
- Mastering Python Design Patterns
- QGIS 2 Cookbook
- AMP:Building Accelerated Mobile Pages
- Java程序設(shè)計(jì)實(shí)用教程(第2版)
- 實(shí)驗(yàn)編程:PsychoPy從入門(mén)到精通
- 游戲設(shè)計(jì)的底層邏輯
- Mastering Machine Learning with R