Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize self-contained pathfinding and Navigation system by unity

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to achieve unity own pathfinding navigation system, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

I. introduction

Unity official documentation:

The navigation grid (Navigation Mesh, abbreviated as NavMesh) is a data structure that describes the walkable surface of the game world and allows you to find a path from one walkable position to another in the game world. The data structure is automatically built or baked from the level geometry.

We can understand it this way: it is an official pathfinding system of unity. We can use it to make simple pathfinding, such as clicking on a location to allow the character to automatically bypass obstacles to the target point, such as enemy AI, so that it can bypass obstacles to pursue our units through NavMesh. You can even set the portal and the starting point of the jump in NavMesh, so that these effects can also participate in the calculation of pathfinding, and successfully calculate the shortcut of navigation.

Introduction to simple use

Simply build a scene and create player and target

/ / Blue-Player Red-Target

Click window-windows-Navigation

Mount the Nav Mesh Agent component on the Player

The Navigation Grid Agent (NavMesh Agent) component helps you create characters that can avoid each other as you move toward a target. Agents use navigation grids to infer the game world and know how to avoid each other and moving obstacles.

Click the ground cube, click the small arrow next to Static, and set it to Navigation Static

Click Navigation, click Bake

You can see that the enemy in the scene where the object can move is baked blue.

If the baking is not successful here, first check to see if the ground is set to Static

If you look at the Gizmos settings of the Scene window, it may have been baked successfully but not displayed.

Write a simple script to mount on the Player ball and tell it where its target is.

Get the Agent component, set the target point through agent.destination, and remember to drag the blue square of the target point into the script

Using UnityEngine;using UnityEngine.AI;public class Player: MonoBehaviour {private NavMeshAgent agent; public Transform target; void Start () {agent = GetComponent (); agent.destination = target.position;}}

In this way, a simple pathfinding little demo is implemented.

Third, detailed introduction of functions (unity2019.4)

Navigation Grid Agent (NavMesh Agent)

Agent Type comes from Navigation and can be set up with multiple different Type.

= = Base offset = = the offset of the collision cylinder from the transform pivot point.

Steering

The maximum moving speed of the Speed in world units per second.

Maximum rotation speed of Angular Speed (degrees per second).

Acceleration maximum acceleration (in world units per square second).

Stopping distance when the distance close to the target location reaches this value, the delegate stops.

Auto Braking when this property is enabled, the agent slows down when it reaches the target. This attribute should be disabled for behaviors such as patrolling, in which case the agent should move smoothly between multiple points

Obstacle Avoidance

The radius of the Radius delegate, which is used to calculate collisions between obstacles and other delegates.

The height clearance required by the Height agent to pass through the overhead obstacle.

Quality obstacle avoidance quality. There are five options from None to High Quality, and if you have a large number of agents, you can save CPU time by reducing the quality of obstacle avoidance. If dodge is set to None, only collisions are resolved and no attempt is made to actively avoid other delegates and obstacles.

When Priority performs obstacle avoidance, this agent ignores agents with lower priority. The value should be in the range of 0-99, where the lower number indicates the higher priority.

Path Finding

Auto Traverse OffMesh Link is set to true to automatically traverse off-grid links (Off-Mesh Link). If you want to use animation or a specific way to traverse off-grid links, you should turn this feature off.

When Auto Repath enables this property, the agent attempts to find its way again when it reaches the end of the partial path. When there is no path to the target, a partial path is generated that leads to the nearest reachable location to the target.

Area Mask Area Mask describes the types of areas that the agent will consider when finding its way. When preparing the grid for navigation mesh baking, you can set each grid area type. For example, you can mark stairs as a special area type and prohibit some character types from using stairs. (for example, I want A to climb stairs and B not to climb stairs, so I can adjust Area Mask to achieve this effect.)

Navigation grid obstacles (NavMesh Obstacle)

Shape optional Capsule or Box determines the shape of the disorder

Select Box:

Center locates obstacles

Size determines the size of the obstacle.

Choose Capsule

Center locates obstacles

Radius determines the radius of obstacles

Height of Height capsule

Carve

When the Carve check box is checked, the navigation grid obstacle creates a hole in the navigation grid.

Move Threshold when the movement distance of a navigation grid obstacle exceeds the value set by Move Threshold, Unity treats it as moving. Use this property to set the threshold distance to update the moved carving hole.

Time To Stationary treats an obstacle as the amount of time (in seconds) required to wait in a stationary state.

Carve Only Stationary when this property is enabled, obstacles are sculpted only when at rest

Considering the performance factors and the limitations of navigation, more practice is needed to adjust the specific parameters.

Off-grid links (Off Mesh Link)

Start describes the object at the beginning of the off-grid link.

End describes the object at the beginning of the off-grid link.

Cost Override if the value is positive, it is used when calculating the path cost for processing path requests. Otherwise, the default cost (the cost of the area to which this game object belongs) is used. If Cost Override is set to a value of 3.0, the cost of moving on an off-grid link is three times the cost of moving the same distance on the default navigation grid area. The Cost Override setting becomes useful if you want the delegate to usually prefer walking, but to use off-grid links when the walking distance is significantly longer.

Bi-Directional if you enable this property, you can traverse the link in either direction. Otherwise, you can only traverse the link in the direction from Start to End.

= = Activated = = specifies whether the pathfinder will use this link (if this property is set to false, it will be ignored).

Auto Update Positions if this property is enabled, the off-grid link will be reconnected to the navigation grid when the endpoint moves. If disabled, the link remains at its starting position even if the endpoint is moved.

Navigation Area describes the type of navigation area for the link. This zone type allows you to apply common traversal costs to similar zone types and prevents some roles from accessing off-grid links based on the proxy's area mask (Area Mask).

Bake page

You can set the navigation height, radius, stair height and ramp angle of Player.

Generated Off Mesh Links

Automatic formation during baking, off-grid links that jump up and down

Note:

The generation of drop links is controlled by the Drop Height parameter. This parameter controls the maximum drop height to be connected, and setting the value to 0 disables generation.

Define the horizontal stroke (A) as: 2agentRadius + 4voxelSize when dropping the linked track. That is, the fall will fall just outside the edge of the platform. In addition, the vertical stroke (B) needs to be greater than the baking setting's Step Height (otherwise it will only walk off the platform) and less than Drop Height. You should adjust to voxel size to ensure that any rounding errors during voxeization do not prevent links from being generated. The value of Drop Height should be set slightly higher than that measured at the level so that the links are connected correctly.

The generation of skip links is controlled by the Jump Distance parameter. This parameter controls the furthest distance to be connected. Setting the value to 0 disables generation.

Define that the horizontal stroke ©should be greater than 2*agentRadius and less than Jump Distance when skipping linked tracks. In addition, the landing position (D) must not exceed the voxelSize of the starting position level.

Areas page

You can create different Areas and set different Cost to give the character a tendency to go through certain routes. For example, although the enemy can directly pass through the gas to catch the protagonist, but because the cost of the gas is high, you can let the enemy bypass the gas to catch the protagonist as far as possible, but if it goes too far, the enemy will still choose to pass through the gas.

The above is all the contents of the article "how to implement unity's own pathfinding and navigation system". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report