When thinking about the AI in Unity random patrols obstacle to predict the rapid movement of physical penetration

If you do not want to use Unity's navigation system, a lot of time is necessary to solve the problem of how to predict obstacles in the path forward, before also seen some very classic example of pathfinding algorithm AStar find its way, though it may be to achieve the function, but the overall feeling some fuss. Most routing algorithm is to obtain the optimal solution, but only if used in a remote area blame the random motion of the body, then simply do not need to use such a complicated algorithm.

 

 

For example, it is strange that the simple remote that it did not want to be close to the players, it's the way sport is going and in any direction within a certain distance, then reached toward the players come out of, and so forth, until it is hit dead. (Hey, this sad fate)

The question then arises, because it is so silly to stay Meng Meng to have been selected towards the next random position, then it is likely to encounter insurmountable obstacles in the way, if this monster can jump, then it better said, I met darted sky obstruction, air 360 to a body 3 and a half weeks, stunning the audience. Unfortunately, it has not Get the skills, so you'll see a guy look on in despair against the barrier and obstacle the light of day never lasting struggle, you start giving it too harsh to add some pre sentenced method obstacles:

1. The simplest and most brutal, can not use the time to direct it, within a period of no movement of distance, it means the obstacle, directly brainwashing re-randomized next target position, if the next random position and there are obstacles, it continues until you can continue to move up. In fact, there should be a better approach for learning, the position of each obstacle after random down, since the range of a radius around the no longer become the next random point.

public  the override the TaskStatus OnUpdate () 
{ 
    IF (Timer> .2f) 
    { 
        IF ((transform.position - posAtlastTimer) .sqrMagnitude < .1f) 
        { 
            // record the current position ... // Re random target position ... 
        } 
        Timer = 0F; 
        posAtlastTimer = transform.position; 
    } 
}

We need a variable position of a timer on the timer and timer recording target.

 

2. Consider using OnCollisionEnter (Collision collision) method to detect, label collision body encountered determination, if it is an obstacle, the current position is recorded and re-randomized. This way the problem can be avoided because Caton repeated to generate a random position of the obstacle.

public  the override  void OnCollisionEnter (Collision Collision) 
{ 
    IF (collision.transform.tag == " Collider " ) 
    { 
        // record the current position ...
         // re-randomized a target position ... 
    } 
}

 

3. Before starting a random movement to the next target point proceeds ray detector, a direction of radiation emitted from the change point to the target point (which may be issued in accordance with a plurality of vertical and horizontal edges of the object), is the current length of the ray location distance to the target position, if the rays hit the obstacle, then you can know ahead of time that the path is invalid.

Private Vector3 RayCheckCollider (Vector3 tarPos) 
{ 
    Vector3 offset = tarPos - transform.position; 
    Ray ray = new new Ray (transform.position, offset); 
    RaycastHit info; 
    IF (Physics.Raycast (ray, OUT info, offset.magnitude)) 
    { 
        IF (info.collider.tag == " Collider " ) 
        { 
            // record the current position ...
             // re-randomized a target position ... 
            tarPos = RayCheckCollider (tarPos); // recursive detection 
        }  
    }
    Debug.DrawLine (ray.origin , tarPos, Color.red);
    return tarPos;
}

 

The above three methods are not in conflict, it can be used simultaneously. The third method is generally better than the first two, it can avoid the encounter obstacles ahead, but doing so often lack a certain authenticity, you can additionally set an optimized field of view, that is, if you can see the obstacles ahead was then the length of the ray radiation obstacle avoidance should be the radius of field of view, once the obstacle during the travel to see the target location, random re next target position.

 

AI invalid by recording the set target position learning can be used to determine the basis for the next target position randomly each time, the set point may be used as all global variables AI determination.

Private  void CheckDeadZone (Vector3 tarPos) 
{ 
    the foreach ( var Item in deadPoints) 
    { 
        IF ((Item - tarPos) .sqrMagnitude < 1F) 
        { 
            // re-randomized a target position ... 
            return ; 
        } 
    } 
}

 

Later found can also be used as a pre-sentence-ray detector in the face of physical problems penetration of fast-moving objects, from the current state of motion when an obstacle or is about to arrive quickly reduce speed, can effectively solve the problem of physical penetration.

Guess you like

Origin www.cnblogs.com/koshio0219/p/11822759.html