Search Unity

Can't make a simple patrol waypoint

Discussion in 'Scripting' started by NAYIR55, Oct 5, 2014.

  1. NAYIR55

    NAYIR55

    Joined:
    Oct 5, 2014
    Posts:
    9
    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 (http://unity3d.com/learn/tutorials/modules/beginner/navigation) 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
     
  2. MysterySoftware

    MysterySoftware

    Joined:
    Sep 18, 2014
    Posts:
    46
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent (typeof(NavMeshAgent))]
    5. public class AIAgent : MonoBehaviour {
    6.     // Reference to NavMeshAgent component
    7.     private NavMeshAgent nav;
    8.  
    9.     // Movement Speed
    10.     public float patrolSpeed = 2.0f;
    11.  
    12.     // Waypoints
    13.     public Transform[] waypoints;
    14.  
    15.     private int curWaypoint = 0;
    16.     private int maxWaypoint;
    17.  
    18.     public float minWaypointDistance = 0.1f;
    19.  
    20.     // When the game starts...
    21.     private void Awake () {
    22.         nav = GetComponent<NavMeshAgent> ();
    23.  
    24.         maxWaypoint = waypoints.Length - 1;
    25.     }
    26.  
    27.     // Every frame...
    28.     private void Update () {
    29.         Patrolling();
    30.     }
    31.  
    32.     public void Patrolling () {
    33.         // Set the ai agents movement speed to patrol speed
    34.         nav.speed = patrolSpeed;
    35.      
    36.         // Create two Vector3 variables, one to buffer the ai agents local position, the other to
    37.         // buffer the next waypoints position
    38.         Vector3 tempLocalPosition;
    39.         Vector3 tempWaypointPosition;
    40.  
    41.         // Agents position (x, set y to 0, z)
    42.         tempLocalPosition = transform.position;
    43.         tempLocalPosition.y = 0f;
    44.  
    45.         // Current waypoints position (x, set y to 0, z)
    46.         tempWaypointPosition = waypoints [curWaypoint].position;
    47.         tempWaypointPosition.y = 0f;
    48.  
    49.         // Is the distance between the agent and the current waypoint within the minWaypointDistance?
    50.         if (Vector3.Distance (tempLocalPosition, tempWaypointPosition) <= minWaypointDistance) {
    51.             // Have we reached the last waypoint?
    52.             if (curWaypoint == maxWaypoint)
    53.                 // If so, go back to the first waypoint and start over again
    54.                 maxWaypoint = 0;
    55.             else
    56.                 // If we haven't reached the Last waypoint, just move on to the next one
    57.                 curWaypoint++;
    58.         }
    59.      
    60.         // Set the destination for the agent
    61.         // The navmesh agent is going to do the rest of the work
    62.         nav.SetDestination (waypoints [curWaypoint].position);
    63.     }
    64. }
     
    Last edited: Oct 13, 2014
  3. NAYIR55

    NAYIR55

    Joined:
    Oct 5, 2014
    Posts:
    9
    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
     

    Attached Files:

    gdossantos87 likes this.
  4. MysterySoftware

    MysterySoftware

    Joined:
    Sep 18, 2014
    Posts:
    46
    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
    Code (CSharp):
    1.             if (curWaypoint == maxWaypoint)
    2.                 // If so, go back to the first waypoint and start over again
    3.                 maxWaypoint = 0;
    4.  
    to
    Code (CSharp):
    1.             if (curWaypoint == maxWaypoint)
    2.                 // If so, go back to the first waypoint and start over again
    3.                 curWaypoint = 0;
    4.  
    so the patrol will be looped.
     
    Last edited: Oct 14, 2014
  5. NAYIR55

    NAYIR55

    Joined:
    Oct 5, 2014
    Posts:
    9
    Oh I see, thank you
    tested it again and it works fine
     
  6. Ruscombmanor

    Ruscombmanor

    Joined:
    Dec 9, 2014
    Posts:
    1
    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.
     
  7. NAYIR55

    NAYIR55

    Joined:
    Oct 5, 2014
    Posts:
    9
    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