Trying to make the unity docs examples of waypoints but it's not working good, How can i fix it ?

The exmaple after reading the whole part of navmeshagent is in this link:

https://docs.unity3d.com/Manual/nav-AgentPatrol.html

In my Hierarchy i have a ThirdPersonController i added to it a NavMeshAgent.
Then added also the waypoints script to the ThirdPersonController.

In the Hierarchy i also have two Spheres as waypoints and Plane.

In the Window > Navigation in tab Object i set both two waypoints and the plane to be Navigation Static.
Then clicked on the Bake button and after some time the baking finished.

Dragged the waypoints to the script in the inspector.

Running the game the player was moving first time to the first waypoint then make a small round and moved to then ext waypoint small round to the start position and then back to the first waypoint but then the player got crazy in the second waypoint again moved strange to the first waypoint and there he is making none stopr rounds around the first waypoint. Strange movments.

The ThirdPersoncontroller walking speed in the animator i set it to 5.0 and created new state called it Walk with HumandoidWalk animation. Set this state as default so when running the game the player is start walking automatic.

Here is a small video clip i recorded showing the strange walking fo the player:

This is a screenshot of the scene after baked:

This is a screenshot of the hierarchy of the thirdpersoncontroller(1) inspector with the navmeshagent and the script attached:

And the waypoints script:

    public Transform[] points;
    private int destPoint = 0;
    private NavMeshAgent agent;

    // Use this for initialization
    void Start () {

        agent = GetComponent<NavMeshAgent> ();
        agent.autoBraking = false;

        GotoNextPoint ();
    }

    void GotoNextPoint()
    {
        if (points.Length == 0)
            return;

        agent.destination = points [destPoint].position;
        destPoint = (destPoint + 1) % points.Length;
    }

    // Update is called once per frame
    void Update () {

        if (agent.remainingDistance < 0.5f)
            GotoNextPoint ();
    }
}

What i want to do is two things:

  1. To fix the problem/s with this waiypoints script.

  2. To make that when the player is getting to the first way point he will turn sharp facing to the second waypoint and will walk directly to the second waypoint in the path between the two waypoints. Same when getting to the second waypoint turn sharp facing to the startposition and then again to the first waypoint. Now what it does is making some round near the waypoints and realy patrolling between them.

You can run something like this in a Coroutine

    IEnumerator Patrolling()
    {
        bool startPatorl = false;

        for (int i = 0; i < waypoints.Length; i++)
        {
            while (!startPatorl)
            {

                if (agent.remainingDistance < 0.5f)
                {
                    i++;
                    dest = i;
                }

                if (i >= waypoints.Length)
                {
                    i = 0;
                    dest = 0;
                }

                agent.destination = waypoints[dest].transform.position;
                yield return null;
            }
        }
    }

it will loop between all the patrol points,

Make sure you call

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

Rob i tried it now didn’t work. The player is walking to the first waypoint and the player is stuttering he get to the first waypoint then stop there i mean he try to keep walking the sphere is blocking it’s way so it’s walking on place instead turning to the next waypoint. I added nav mesh agaent to the thirdpersoncontroller and attached the script and also baked it again.

Have you ticked the spheres as “Not Walkable” before baking/

Edit : Just tested my script in a new project and it worked fine

Now it’s working once i changed the remainingDistance from 0.5f to 2.5f but still the player is stuttering not sure why.

Rob your script in your side the remainingDistance is set to 0.5f ? I need to make the spheres not walkable ? Meaning to tick so it will not be Navigation Static ?

Ok i see now i changed both to Not Walkable i’m baking it now again.

Let me know if sorts the problem!

It’s working by walking between the waypoints but still stuttering.
I will try to work out the stuttering and will update.

Are the colliders disabled on the spheres?

Can’t figure out why it’s stuttering. When i set the camera as target to follow other thirdpersoncontroller it’s not stuttering.

Tried to diable now in both spheres ticked the checkboxes near Sphere Collider it didn’t help.

Rob i checked now if i disable the script on the thirdpersoncontroller it will walk fine without stuttering. Once i tick to use the script it’s stuttering. Here is a shot video clip i recorded now show thw problem when the script is on:

Is the player stuttering or the camera? can’t really tell haha

The problem is that the variable startPatrol never set to true it’s always false. Rob can you check it ? In your script you set somewhere the startPatrol to true ?

we’d need to see the character moving in the scene view to be able to tell (usually it’s not that useful to show a bug in “game fullscreen”)

Its always set to false because it’s a neverending loop for the patrols, it shouldn’t have an effect on the stuttering, try setting your thirdpersoncontroller rigidbody to iskinematic