Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Agent getting stuck in plane

Discussion in 'Navigation' started by markblank05, Apr 5, 2015.

  1. markblank05

    markblank05

    Joined:
    Apr 2, 2015
    Posts:
    29
    Hi, I'm testing the navmesh and ai, but I'm having a problem with my agent, they get stuck in plane and I don't know what is the reason or how to fix it. And how can I make the agent chose different path if he cant go to the setdestination?

    Here is the videos of my navmesh, the problem happens arround 1min. My ai will look randomly in the area and when the cube enters the sphere collider, my agent will go the cube position and destroy it then instantiate another prefab

    the lag is only in recording. thanks




    Here is my codes

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AgentScript : MonoBehaviour
    5. {
    6.     public Rigidbody zombie;
    7.     public float fieldOfView = 110f;
    8.  
    9.     NavMeshAgent agent;
    10.     SphereCollider col;
    11.     Vector3 target;
    12.     Vector3 lastPosition;
    13.  
    14.     void Start ()
    15.     {
    16.         agent = GetComponent<NavMeshAgent> ();
    17.         col = GetComponent<SphereCollider> ();
    18.     }
    19.  
    20.     void Update ()
    21.     {
    22.         randomWalk ();
    23.         Debug.DrawLine (transform.position, target, Color.red);
    24.     }
    25.  
    26.  
    27.     void randomWalk()
    28.     {
    29.         if (!agent.pathPending)
    30.         {
    31.             if(agent.remainingDistance <= agent.stoppingDistance)
    32.             {
    33.                 if (!agent.hasPath || agent.velocity.sqrMagnitude == 0f)
    34.                 {
    35.                     Vector3 randomDirection = Random.insideUnitSphere * col.radius;
    36.                     randomDirection += transform.position;
    37.                     NavMeshHit hit;
    38.                     NavMesh.SamplePosition(randomDirection, out hit, col.radius, 1);
    39.                     target = hit.position;
    40.  
    41.                     agent.SetDestination(target);
    42.                 }
    43.             }
    44.         }
    45.     }
    46.  
    47.     void OnCollisionEnter(Collision other)
    48.     {
    49.         if (other.gameObject.tag == "Player")
    50.         {
    51.             Destroy (other.gameObject);
    52.             agent.ResetPath();
    53.             lastPosition = target;
    54.  
    55.             Instantiate(zombie, lastPosition, Quaternion.identity);
    56.         }
    57.     }
    58.  
    59.     void OnTriggerEnter(Collider other)
    60.     {
    61.  
    62.     }
    63.  
    64.     void OnTriggerStay(Collider other)
    65.     {
    66.         if (other.gameObject.tag == "Player")
    67.         {
    68.             Vector3 direction = other.transform.position - transform.position;
    69.             float angle = Vector3.Angle(direction, transform.forward);
    70.  
    71.             if (angle < fieldOfView * 0.5f)
    72.             {
    73.                 RaycastHit hit;
    74.            
    75.                 if(Physics.Raycast(transform.position, direction.normalized, out hit, col.radius))
    76.                 {
    77.                     if(hit.collider.tag == "Player")
    78.                     {
    79.                         target = hit.collider.transform.position;
    80.                         agent.SetDestination (target);
    81.                     }
    82.                 }
    83.             }
    84.          }
    85.     }
    86.  
    87.  
    88.     void OnTriggerExit(Collider other)
    89.     {
    90.         if (other.gameObject.tag == "Player")
    91.         {
    92.             Debug.Log ("Exit");
    93.         }
    94.     }
    95.  
    96.  
    97. }
    98.  
     
    Last edited: Apr 17, 2015