1. 程式人生 > >Unity 之Navigation烘焙

Unity 之Navigation烘焙

Building a NavMesh

在Unity中,NavMesh  的生成操作需要Navigation視窗(在Window> Navigation

在你的場景中構建NavMesh只需要4個步驟:

  1. 選擇場景中需要生成尋路的幾何體-可行走表面和障礙物。
  2. 在NavMesh面板中選擇需要烘焙尋路的物體,檢測是否勾選Navigation Static.
  3. 根據你的agent大小來調整bake 面板的設定。
    • Agent Radius : agent可以距離牆體 ,窗戶或邊緣多近的距離。
    • Agent Height : agent可以通過的最低的空間高度。
    • Max Slope : agent可以直接行走上去的最小坡度。
    • Step Height:  agent可以踩上(走上)的障礙物最高高度。
  4. 點選bake按鈕烘焙NavMesh。

當你的Navigation視窗開啟並且可見時,烘焙的NavMesh結果在場景中會以藍色的覆蓋層在物體的幾何體表面顯示。

When baking is complete, you will find a NavMesh asset file inside a folder with the same name as the scene the NavMesh belongs to. For example, if you have a scene called First Level

 in the Assets folder, the NavMesh will be at Assets > First Level > NavMesh.asset.當烘焙結束後,你可以找到一個名字和你的場景名一樣的資料夾,裡面有一個NavMesh的資原始檔,是屬於這個場景的NavMesh。

讓物體可烘焙的其他方法

直接選擇物體在Inspector面板頂部的Static 選單,你可以直接選擇Navigation Static,而不用再開啟Navigation 視窗。

NavMesh烘焙的高階設定

最小區域面積

Min Region Area 允許你剔除掉小的非連線NavMesh區域,當NavMesh區域小於指定值時將被剔除。

請注意:有些區域可能無法被移除,儘管有Min Region Area 的設定,原因是NavMesh的構建是一個個的網格並行構建。當一個區域橫跨兩個網格將不會被移除,因為區域修剪過程中無法獲取到周圍的網格。

Voxel Size 立體畫素尺寸

Manual voxel size :允許你改變烘焙操作過程中的精確性。

Navigation在構建尋路網格過程中,第一遍會把場景光柵化為體素,然後提取可行走區域,最後可行走區域會烘焙成網格。因此體素尺寸Voxel Size的決定了尋路網格烘焙的精準性。

提示:預設的體素設定最好的權衡了準確性和烘焙速度。在烘焙場景尋路的過程中,體素的增加會造成4x倍的記憶體消耗和4x倍的時間消耗。因此通常,你不需要自己去設定Voxel Size。

Smaller Agent Radius

系統烘焙尋路是也會減小voxel size。如果你的agent尺寸保持不變,可能不需要增加NavMesh的構建解析度。

更簡單的方法如下所示:

  1. 設定你的半徑為真實agent半徑
  2. 開啟Manual Voxel Size,這會保持當前的voxel的大小並且凍結它。
  3. 人為的將你的Agent Radius設定小,因為你已經勾選了Manual Voxel Size ,voxel size將不會改變。

Height mesh : 允許你為角色提供更精準的位置。

While navigating, the NavMesh Agent is constrained on the surface of the NavMesh. Since the NavMesh is an approximation of the walkable space, some features are evened out when the NavMesh is being built. For example, stairs may appear as a slope in the NavMesh. If your game requires accurate placement of the agent, you should enable Height Mesh building when you bake the NavMesh. The setting can be found under the Advanced settings in Navigation window. Note that building Height Mesh will take up memory and processing at runtime, and it will take a little longer to bake the NavMesh.

儘管普通的NavMesh 已經可以很好的執行,但是由於NavMesh只是一個近似的可行走的空間,只保持了一些均衡的特性,如果你的遊戲agent需要更精準的行走位置,你可以啟用高度網格Height Mesh,

注意:高度網格Height Mesh將佔用你“執行時”的記憶體和cpu,並且需要更多的烘焙時間。