Can't make a simple patrol waypoint

Hello
First of all, I’m very newb in this and I really want to know how to make games and how how to script them as well…
To the point…
Being absolutly newb I was reading the documentation and watching the official tuts about navigation and navmesh, until I got stucked wanting to create a simple patrol pattern using waypoints, unfortunately the documentation about -navmeshpath, nextposition- and almost everything related to navigation, pathing and waypoints, lacks content and useful info.
I watched a video (Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn) about navmesh and works fine, but I don’t know how to use the navmesh tools that I need to create a very simple waypoint patrol pattern.
Can someone please explain how to use the navmesh pathing tools??
PD. I’m using C# script
Thanks

Took the liberty of writing you a simple C# script, you could attach to the agent/character you want to move along the waypoints.
You’ll just have to set a few waypoints, and create a navmesh.
I recommend you watch this video (http://unity3d.com/learn/tutorials/modules/beginner/navigation/navmesh-baking)
and then try to get your agent/character running.

using UnityEngine;
using System.Collections;

[RequireComponent (typeof(NavMeshAgent))]
public class AIAgent : MonoBehaviour {
    // Reference to NavMeshAgent component
    private NavMeshAgent nav;
 
    // Movement Speed
    public float patrolSpeed = 2.0f;
 
    // Waypoints
    public Transform[] waypoints;
 
    private int curWaypoint = 0;
    private int maxWaypoint;
 
    public float minWaypointDistance = 0.1f;
 
    // When the game starts...
    private void Awake () {
        nav = GetComponent<NavMeshAgent> ();

        maxWaypoint = waypoints.Length - 1;
    }
 
    // Every frame...
    private void Update () {
        Patrolling();
    }
 
    public void Patrolling () {
        // Set the ai agents movement speed to patrol speed
        nav.speed = patrolSpeed;
     
        // Create two Vector3 variables, one to buffer the ai agents local position, the other to
        // buffer the next waypoints position
        Vector3 tempLocalPosition;
        Vector3 tempWaypointPosition;

        // Agents position (x, set y to 0, z)
        tempLocalPosition = transform.position;
        tempLocalPosition.y = 0f;

        // Current waypoints position (x, set y to 0, z)
        tempWaypointPosition = waypoints [curWaypoint].position;
        tempWaypointPosition.y = 0f;

        // Is the distance between the agent and the current waypoint within the minWaypointDistance?
        if (Vector3.Distance (tempLocalPosition, tempWaypointPosition) <= minWaypointDistance) {
            // Have we reached the last waypoint?
            if (curWaypoint == maxWaypoint)
                // If so, go back to the first waypoint and start over again
                maxWaypoint = 0;
            else
                // If we haven't reached the Last waypoint, just move on to the next one
                curWaypoint++;
        }
     
        // Set the destination for the agent
        // The navmesh agent is going to do the rest of the work
        nav.SetDestination (waypoints [curWaypoint].position);
    }
}

Thank you very much!!
I tested it and works fine, now I have a question…
the agent doesn’t go back to the origin, is it normal? or I need to put a waypoint in the origin??
Here’s an image of my result

1 Like

The agent is just going to go along the waypoints. It’s not going to go back to it’s origin, you’ll have to set a waypoint for that.

Oh. And I made a slight mistake. You’ll have to change

            if (curWaypoint == maxWaypoint)
                // If so, go back to the first waypoint and start over again
                maxWaypoint = 0;

to

            if (curWaypoint == maxWaypoint)
                // If so, go back to the first waypoint and start over again
                curWaypoint = 0;

so the patrol will be looped.

Oh I see, thank you
tested it again and it works fine

How do you assign waypoints to the script? I made an empty game object and named them waypoints. But I am not sure how to assign them so that the agent goes to them.

You have to make a public array of waypoint, then when you have it, it’ll show in the inspector the rectangles of the waypoints, it’ll say “transform” and it’s looking for objects, at the beginning there’s the code, copy and paste and see what happens