Issue with Agentmesh

Hi, I have a simple sandbox created with a player and a few cubs patrolling through a set of waypoints that I have stored in a list. It is working fine using the following code to give patrols their target destination…

transform.position = Vector3.MoveTowards (transform.position, currentWaypoint.position, speed * Time.deltaTime);

I have then decided to add a nav mesh so as to include some obstacles for the patrols. To do this, I added a nav agent to the patrol cubes and then use the following code to set their target destination…

navAgent.SetDestination(currentWaypoint.position);

I find when I use this navAgent.SetDestination that the cube reaches the first waypoint and then tips over and gets stuck. I have tried to freeze rotation in the inspector but that seems to make no difference.

Here is the code for the function I have created.

    public void moveEnemy()
    {

        if (GameObject.Find ("Player") != null && PT.isDetected == true) {

            Debug.Log ("The Player is in detected!");

            currentPatrolTarget = GameObject.Find ("Player").transform;
            navAgent.SetDestination(currentPatrolTarget.position);
            //transform.position = Vector3.MoveTowards (transform.position, currentPatrolTarget.transform.position, speed * Time.deltaTime);
        } else
        {
            float distanceToCurrent = Vector2.Distance (transform.position, currentWaypoint.position);

            if (distanceToCurrent == 0) {
                if (wayPointNumber != wayPointsList.Count - 1) {
                    wayPointNumber++;
                    currentWaypoint = wayPointsList [wayPointNumber];
                } else {
                    wayPointNumber = 0;
                    currentWaypoint = wayPointsList [wayPointNumber];
                }
            }
            navAgent.SetDestination(currentWaypoint.position);
            //transform.position = Vector3.MoveTowards (transform.position, currentWaypoint.position, speed * Time.deltaTime);

            //Establish the transform to give the raycast a target to rotate too
            Vector3 relativePos = currentWaypoint.position - transform.position;
            transform.rotation = Quaternion.LookRotation (relativePos);

            // targetRotation would be the result of your Quat.LookRotation call
//        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
        }
    }

Any thoughts or advice appreciated.

2h

What is moveEnemy called? Is it called as part of an Update loop?

The method you’re using to check the distance to the current way point is not good enough.
Replace your distanceToCurrent with the following:

if(navAgent.remainingDistance <= navAgent.stoppingDistance)
{
    if (wayPointNumber != wayPointsList.Count - 1) {
        wayPointNumber++;
        navAgent.SetDestination(wayPointsList [wayPointNumber].position);
    } else {
        wayPointNumber = 0;
        navAgent.SetDestination(wayPointsList [wayPointNumber].position);
    }

}

You will find a good example of this in the Stealth project.
Specifically the EnemyAI component which i think you could probably just use verbatim for what you need.

1 Like

Hi Bob, thanks for the reply.

Yes, I have moveEnemy called in the update method.

    void Update () {
        moveEnemy ();
    }

2h

No probs.
That being the case there are a number of issues with your moveEnemy method if I’m honest.
Using GameObject.Find in an Update loop is bad. You should create a reference to the player in Awake() and use that.

Also you’re setting the NavAgent’s destination each frame which is also not great. You should only set the destination when the target needs to change.

I’ve updated my post with a link to the EnemyAI script from the Stealth tutorial. I recommend you use that and adapt for what you need. It’s very robust and follows some good practices.

1 Like

Thanks pBob.

The reason I used the GameObject.find was that I had an issue when the player died as I was destroying the object. It’s lack of existence at that stage had implications for other scripts, so I then altered things so that they only ran as long as the player was known to exist.

I will look into the issues you have mentioned, ref in Awake().

I have the patrol setup so that if the player is detected, it will reassign it’s destination to the players position as long as the player is visible to the patrol. To do this I’m guessing that I have to keep updating the NavAgent destination to follow the player?

I will have a look at the script you have linked.

Thank you very much for the help, I really appreciate it.

2h