Dynamic Collision of navmesh System

Source: Internet
Author: User

The navmesh system provided by unity3d can easily solve the game Path-seeking problem, but the system has a problem that is hard to understand:

  In navmesh navigation, the physics system collision will be ignored. That is to say, navmeshagent will not be blocked by collider while moving, but will go directly (but ontriggerenter and other trigger functions are normal ).

  The Dynamic Collision function is a basic requirement for many games. According to the interfaces provided by navmesh, navmeshobstacle is the only one that can implement the blocking function, while navmeshobstacle has only one shape: a cylinder, in addition, the UP direction is fixed and cannot be adjusted to the lateral direction. The following points are summarized:

(1) The walking/collision area of the navigation mesh can only be pre-baked;

(2) The Dynamic Collision body can only be achieved by attaching the navmeshobstacle component;

(3) The collision body has only one shape-a cylinder, which is strictly a circular shape and cannot be an elliptical shape.

So here, we can basically give up using collider of various shapes to create a scenario blocker. However, there are still alternative solutions:If you must use the navmesh provided by unity3d for navigation, you can use the circle as the basic element to simulate other shapes.

  

  It shows how to simulate a cube blocker through navmeshobjstacle. to conveniently edit the cube size, you can write an auxiliary script to implement it:

Using unityengine; using system. collections; using system. collections. generic; [executeineditmode] public class multiobstaclehelper: monobehaviour {public float interval = 1f; // interval between obstacle public int num = 1; // Number of obstacle private float curinterval = 1f; private int curnum = 1; private transform template = NULL; void awake () {template = gameobject. transform. find ("obstacle");} void start () {ad Just ();} void Update () {If (Num <= 0) num = curnum; adjust ();} private void adjust () {If (template = NULL) return; adjustinterval (adjustnum ();} private bool adjustnum () {If (curnum = num) return false; If (Num> curnum) {for (INT I = 0; I <num-curnum; ++ I) {gameobject go = gameobject. instantiate (template. gameobject) as gameobject; go. transform. parent = template. parent; go. transform. l Ocalposition = vector3.zero; go. transform. localscale = vector3.one; go. transform. localrotation = Quaternion. identity ;}} else if (Num <curnum) {int COUNT = curnum-num; List <transform> lst = new list <transform> (); For (INT I = 0; I <template. parent. transform. childcount; ++ I) {If (count <= 0) break; If (template. parent. getchild (I )! = Template) {lst. add (template. parent. getchild (I); count -- ;}} while (lst. count> 0) {transform TRAN = lst [0]; gameobject. destroyimmediate (Tran. gameobject); lst. removeat (0);} lst. clear () ;}curnum = num; return true;} private void adjustinterval (bool numchange) {If (numchange = false & curinterval = interval) return; int half = num/2; int Index = 0; foreach (transform Tran in template. parent. gameobject. transform) {// an odd number of IF (Num % 2 = 1) {vector3 Pos = Tran. localposition; POS. X = (index-half) * interval; Tran. localposition = Pos;} else {vector3 Pos = Tran. localposition; POS. X = (index-half + 0.5f) * interval; Tran. localposition = Pos;} index ++;} curinterval = interval ;}}

The code above can adjust the number and spacing of the obstacle, and then adjust the scaling ratio together to basically make cubes of various sizes.

  Implementation of one-way blocking,You can combine trigger and navmeshobstacle to implement one-way blocking:

  

The implementation idea is that when the role enters the red trigger area, it will hide the block and activate it after 1 second, so that it can implement a one-way block function, the implementation code is relatively simple, as shown below:

Using unityengine; using system. collections; # If your unityeditor; # endifpublic class singlepasstrigger: monobehaviour {[hideininspector] public transform object = NULL; Public transform collider = NULL; public float passtime = 1f; void start () {object = transform. parent. transform. find ("object"); collider = transform. parent. transform. find ("collider");} protected virtual void ontrigger Enter (collider other) {stopcoroutine ("letpasscoroutine"); startcoroutine ("letpasscoroutine");} protected virtual void ontriggerexit (collider other) {} ienumerator quit () {setpassstate (true); float starttime = time. time; while (time. time <starttime + passtime) {yield return NULL;} setpassstate (false);} private void setpassstate (bool value) {If (collider = NULL) return; Collider. Gameobject. setactive (! Value) ;}# if unity_editor void ondrawgizmos () {// set the rotation matrix matrix4x4 rotationmatrix = matrix4x4. TRS (vector3.zero, transform. rotation, vector3.one); gizmos. matrix = transform. localtoworldmatrix; // draw a standard-sized object gizmos at the local coordinate origin. color = new color (1f, 0f, 0f, 0.8f); gizmos. drawcube (vector3.zero, vector3.one); gizmos. color = color. black; gizmos. drawwirecube (vector3.zero, vector3.one); gizmos. drawicon (transform. position + vector3.up, "ban.png") ;}# endif}

 

  

Dynamic Collision of navmesh System

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.