Search Unity

Why the navmeshagent is not walking through the door ?

Discussion in 'Navigation' started by DubiDuboni, Jun 9, 2020.

  1. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    The nav mesh agent have this components : Animator , Nav Mesh Agent , Box Collider , Agent Control(script) and a Rigidbody and both Use Gravity and Is Kinematic are set to true enable true.

    When the character is getting close to the door the character stop and is not walking through. but if I disable the Nav Mesh Agent component I can drag the character through the door. but disabling the Nav Mesh Agent component is not a solution.

    In the Navigation window I selected the door and set the door to be Navigation static and Walkable did it for all the door children then baked again but it didn't help much.

    Screenshot of the agent near the open door and if I try to drag the agent in the scene view through the door he will not move through only if I disable the Nav Mesh Agent component first :



    The waypoint the agent should get to is in the next room the blue teleporter.

    This is the script that make the agent move to the waypoint :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.AI;
    6.  
    7. public class AgentControl : MonoBehaviour
    8. {
    9.    public List<Transform> points;
    10.    public bool waitTimeToMove = false;
    11.    //notice WaitTime is a float now
    12.    public float WaitTime = 10f;
    13.    public bool randomWaitTime;
    14.    float waitMinTime = 1f;
    15.    float waitMaxTime = 10f;
    16.    public bool loop = false;
    17.    public Animator anim;
    18.  
    19.    private int destPoint = 0;
    20.    private NavMeshAgent agent;
    21.    private Transform originalPos;
    22.    //two vars for handling timer
    23.    private float timer = 0;
    24.    private float originSpeed;
    25.  
    26.    void Start()
    27.    {
    28.        agent = GetComponent<NavMeshAgent>();
    29.  
    30.        // Disabling auto-braking allows for continuous movement
    31.        // between points (ie, the agent doesn't slow down as it
    32.        // approaches a destination point).
    33.        agent.autoBraking = false;
    34.  
    35.        originSpeed = agent.speed;
    36.  
    37.        if (randomWaitTime == true)
    38.        {
    39.            WaitTime = Random.Range(waitMinTime, waitMaxTime);
    40.        }
    41.  
    42.        //transforms dont exist without A GameObject and a GameObject doesn't exist without a transform
    43.        //create a new GameObject to hold our position
    44.        GameObject originalPositionObject = new GameObject();
    45.        originalPositionObject.name = "WP";
    46.        originalPositionObject.tag = "Waypoint";
    47.        originalPositionObject.transform.parent = GameObject.Find("Waypoints").transform;
    48.        //set the new gameobjects position equal to where the transform is right now
    49.        originalPositionObject.transform.position = transform.position;
    50.        //add this to the points list instead
    51.        points.Add(originalPositionObject.transform);
    52.  
    53.        anim = GetComponent<Animator>();
    54.    }
    55.  
    56.  
    57.    void GotoNextPoint()
    58.    {
    59.        // Returns if no points have been set up
    60.        if (points.Count == 0)
    61.            return;
    62.  
    63.        // Set the agent to go to the currently selected destination.
    64.        agent.destination = points[destPoint].position;
    65.  
    66.        // Choose the next point in the array as the destination,
    67.        // cycling to the start if necessary.
    68.        destPoint = (destPoint + 1) % points.Count;
    69.    }
    70.  
    71.  
    72.    void Update()
    73.    {
    74.        // Choose the next destination point when the agent gets
    75.        // close to the current one.
    76.        if (!agent.pathPending && agent.remainingDistance < 1f)
    77.        {
    78.            //if wait to move is true
    79.            if (waitTimeToMove)
    80.            {
    81.                //if timer is less than 10
    82.                if (timer < WaitTime)
    83.                {
    84.                    //add Time.deltaTime each time we hit this point
    85.                    timer += Time.deltaTime;
    86.                }
    87.                //no longer waiting because timer is greater than 10
    88.                else
    89.                {
    90.  
    91.                    waitTimeToMove = false;
    92.  
    93.                    anim.SetBool("Walk", true);
    94.                }
    95.            }
    96.            //if we hit here waitToMove is false, so go ahead as usual
    97.            else
    98.            {
    99.                if (loop == false && destPoint == points.Count - 1)
    100.                {
    101.                    anim.SetBool("Idle", true);
    102.                    agent.speed = 0;
    103.                }
    104.  
    105.                if (loop == true || destPoint != points.Count - 1)
    106.                {
    107.                    agent.speed = originSpeed;
    108.  
    109.                    GotoNextPoint();
    110.                }
    111.            }
    112.        }
    113.    }
    114. }
    115.  
     
  2. warthos3399

    warthos3399

    Joined:
    May 11, 2019
    Posts:
    1,749
    Theres no NavMesh on that door, or any part of it, or it would be light blue color like the rest of the NavMesh.