Getting started with automatic pathfinding Navmesh basics

Source: Internet
Author: User

Auto Pathfinding Navmesh

Most of the games now have an automatic pathfinding feature. When you click a location on the scene, the character automatically finds its way past. There may be a lot of obstacles in the middle, and the character will automatically bypass the obstacles and eventually reach the end. Using unity to develop a hand tour, automatic pathfinding can be implemented in many ways. The first more traditional is the use of a-star pathfinding, which is a more traditional artificial intelligence algorithm, in the game development is more commonly used. This technique is used in most of the page tours and end trips. In the Unity game can also use this technology, Asset store above already have related components, interested students can get to know. I'll have a chance to come back in detail later. Today we are going to learn Unity's official built-in pathfinding plugin-navmesh. Start by learning one of the simplest examples today.

An object can use the Pathfinding function as long as it has transform.

? Example

? We want to implement a function: Click a location in the scene, the character can automatically find the way past. The character bypasses all kinds of complex obstacles and finds a theoretical "shortest path".

? Steps

? 1. Create a terrain

? 2. Add a role

? 3. Create multiple obstacles and try to make it as complex as possible to check the availability and efficiency of the Navmesh.

? 4. Select the terrain, and in the navigation window, set the navigation Static

? 5. Select the obstacle, and in the Avigation window, set the navigation Static

? 7.Navigation window, choose the bake (baking) interface, click the Bake button, process scene baking, you can bake out the road to the grid.

? 8. Add the Navmeshagent (mesh proxy pathfinding) component for the role. Component->navigation->navmesh Agent

9. Add a script PlayerController.cs to the character, achieve click Target, auto pathfinding function

Related knowledge

1. The nav Mesh Agent component on the persona

Radius radius: The radius of the agent (for pathfinding purposes only, which can be different from the radius of the actual object, generally larger than the radius of the actual object).

Speed: The agent can travel around the world and move to its destination at maximum speed.

Acceleration acceleration: maximum acceleration.

Angular speed angular velocity: maximum speed (degrees/sec).

Stoppingdistance braking distance: braking distance. The distance to the destination is less than this value and the agent slows down.

Auto Traverseoffmesh link automatically traverses Offmesh Links: auto-move and close offmeshlinks

Auto Repath automatically re-pathfinding: If the existing part is invalidated, obtain a new path.

Height altitude: The height of the agent (for debugging graphics).

Base offset basic shift: The collision geometry is offset vertically relative to the actual geometry.

Obstacleavoidance type obstacle avoidance types: The quality level of evasion.

Navmeshwalkable Navigation grid Walk: Specifies the type of navigation grid layer that the agent can traverse. This parameter is useful and can be used in the following example .

2. The barrier must have mesh Render, used to bake the road-finding grid.

? Summary

This example can be very simple for us to learn how to use the auto-pathfinding component Nav in the most basic way. However, this component also provides more powerful functions, such as blocking between the starting point and the target point,

What to do? In the following article, we will continue to explore other powerful features.

Instance:

usingUnityengine;
usingSystem.Collections;

Public classWalk:Monobehaviour {
     PublicAnimator AA;
     Public Cameracc;
     Public Vector3targetposition;
     Public navmeshagentnav;
    voidStart () {
targetposition= This. transform.position;
aa= This. Getcomponent<animator> ();
nav= This. getcomponent<navmeshagent> ();
    }
    

    voidUpdate () {
nav. Setdestination (targetposition);
        if(Input. Getmousebuttondown (0)){
AA. Play ("Skeleton_walk");
GetPoint ();
        }
        if(nav.remainingdistance<0.3f){
AA. Play ("Skeleton_idle");
nav. Stop ();
        }
    }

    voidGetPoint () {
        RayRay;
        RaycasthitHit ;
RAY=CC. Screenpointtoray (Input. mouseposition);
        if(Physics. Raycast (Ray, outHit )) {
            if(hit.collider.gameobject.tag=="Road"){
                //debug.log (hit.point);
Targetposition=hit.point;
            }
        }
    }
}

Getting started with automatic pathfinding Navmesh basics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.