0
\$\begingroup\$

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 :

Agent

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 :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class AgentControl : MonoBehaviour
{
    public List<Transform> points;
    public bool waitTimeToMove = false;
    //notice WaitTime is a float now
    public float WaitTime = 10f;
    public bool randomWaitTime;
    float waitMinTime = 1f;
    float waitMaxTime = 10f;
    public bool loop = false;
    public Animator anim;

    private int destPoint = 0;
    private NavMeshAgent agent;
    private Transform originalPos;
    //two vars for handling timer
    private float timer = 0;
    private float originSpeed;

    void Start()
    {
        agent = GetComponent<NavMeshAgent>();

        // Disabling auto-braking allows for continuous movement
        // between points (ie, the agent doesn't slow down as it
        // approaches a destination point).
        agent.autoBraking = false;

        originSpeed = agent.speed;

        if (randomWaitTime == true)
        {
            WaitTime = Random.Range(waitMinTime, waitMaxTime);
        }

        //transforms dont exist without A GameObject and a GameObject doesn't exist without a transform
        //create a new GameObject to hold our position
        GameObject originalPositionObject = new GameObject();
        originalPositionObject.name = "WP";
        originalPositionObject.tag = "Waypoint";
        originalPositionObject.transform.parent = GameObject.Find("Waypoints").transform;
        //set the new gameobjects position equal to where the transform is right now
        originalPositionObject.transform.position = transform.position;
        //add this to the points list instead
        points.Add(originalPositionObject.transform);

        anim = GetComponent<Animator>();
    }


    void GotoNextPoint()
    {
        // Returns if no points have been set up
        if (points.Count == 0)
            return;

        // Set the agent to go to the currently selected destination.
        agent.destination = points[destPoint].position;

        // Choose the next point in the array as the destination,
        // cycling to the start if necessary.
        destPoint = (destPoint + 1) % points.Count;
    }


    void Update()
    {
        // Choose the next destination point when the agent gets
        // close to the current one.
        if (!agent.pathPending && agent.remainingDistance < 1f)
        {
            //if wait to move is true
            if (waitTimeToMove)
            {
                //if timer is less than 10
                if (timer < WaitTime)
                {
                    //add Time.deltaTime each time we hit this point
                    timer += Time.deltaTime;
                }
                //no longer waiting because timer is greater than 10
                else
                {

                    waitTimeToMove = false;

                    anim.SetBool("Walk", true);
                }
            }
            //if we hit here waitToMove is false, so go ahead as usual
            else
            {
                if (loop == false && destPoint == points.Count - 1)
                {
                    anim.SetBool("Idle", true);
                    agent.speed = 0;
                }

                if (loop == true || destPoint != points.Count - 1)
                {
                    agent.speed = originSpeed;

                    GotoNextPoint();
                }
            }
        }
    }
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ It looks like there's no navmesh connection through the doorway. Do you have a collider there that the navmesh might read as an obstacle, or is the door less than twice your pathfinding radius wide? \$\endgroup\$
    – DMGregory
    Commented Jun 8, 2020 at 23:38

1 Answer 1

-1
\$\begingroup\$

Try adjusting the height and width of the Nav Mesh Agent.

\$\endgroup\$
1
  • \$\begingroup\$ This answer would be better if it included some specific details of what adjustments to make, or how to gauge what the right settings should be for this situation. \$\endgroup\$
    – DMGregory
    Commented Jun 11, 2020 at 16:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .