Unity primary AI avoids obstacles

Source: Internet
Author: User

1. Introduction

After reading the 6th chapter of Unity Game AI programming, it is not very helpful to feel the obstacle avoidance algorithm. To study and learn unity, improve the algorithm for avoiding obstacles. Of course, the code is not optimized, but it doesn't matter, to record ideas, learn to communicate.


2. Changes compared to the original book algorithm:

1. Enable the physical engine to use speed and force to solve the problem. Instead of using the angle and position

2. Join the walk along the obstacle, even if the obstacle width is very large or the object moves to the obstacle speed too fast, also will not occur through the wall phenomenon.

3. In order to fit along the obstacle walk, and appear more natural, introduce three speed and obstacle of interactive partition.


3. Share the source code:

public void Avoidobstacles (ref Vector3 dir) {raycasthit hit;//only detect layer 8 (obstacles) int layermask = 1 << 8; Check that the vehicle hits with the obstacles within it's minimum distance to Avoid//print (transform.forward);//obstacle Interaction Area I F (Physics.raycast (Transform.position, rb.velocity, out hit, Minimumdisttoavoid, Layermask)) {Alongwall = false; Vector3 Hitnormal = Hit.normal;hitnormal.y = 0.0f; Don ' t want to move in y-spacedistoavoid = hit.distance;//[5-minimumdisttoavoid] exclusion zone if (Distoavoid > 5) {print ("exclusion zone"); if (Rb.velocity.sqrMagnitude >) RB. Addforce (Hitnormal * 10);} [2-5.0] Traction Zone else if (distoavoid > 2f) {print ("traction zone"); RB. Addforce (Vector3.cross (Hitnormal, vector3.up). Normalized * 5);//return;} [0,2] Parallel region else {print ("parallel zone"); Alongwall = True;rb.velocity = Vector3.cross (Hitnormal, vector3.up). normalized * 10;           Alonghitnormal =-hitnormal;//return;} }//target interaction area else if (Physics.raycast (Transform.position, dir, out hit, Vector3.distance (transform.position, TargetPoint), Layermask) {if (Alongwall) {if (! Physics.raycast (Transform.position, Alonghitnormal, out hits, 2.0f, Layermask)) {print ("Out of parallel zone, start turning"), RB. Addforce (dir * 10);}}  else if (hit.distance>minimumdisttoavoid) {print ("normal"); rb.velocity=dir*8;} } else {print ("unimpeded"); rb.velocity = dir * 10;}}
This algorithm still has flaws, the biggest problem is the computational volume is large, and the details are not rich enough, resulting in the movement of objects unnatural.


4. Algorithm detailed Description:

When the obstacle is gradually approached, the repulsion zone is first entered, and the repulsive force is applied to the moving animal in the plane normal direction of the obstacle. Again the approximation is applied to the guiding force parallel to the obstacle plane. If it is approached again, the velocity of the moving object is changed directly to the velocity parallel to the obstacle plane. The velocity parallel to the obstacle plane is calculated by the Vector fork, and the left-handed coordinate system is observed.


When an object moves along an obstacle, it does nothing until it is detached from the obstruction and begins to exert force toward the target point. If the object does not move along the wall, and there is no obstacle to the target within a certain range. Change the speed directly.

If the target point is found to be unobstructed, change the speed directly to move forward.


5. Precautions:

Put this code into the original book project, can test the effect. One problem to note is that, because of the volume of moving objects, the mere position of the object will collide at the corner, and to simplify the problem, turn on the Moving object Istrigger option.


Unity primary AI Dodge obstacles

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.