Search Unity

Basic Waypoint Example

Discussion in 'Community Learning & Teaching' started by mkjrfan, Jan 18, 2014.

  1. mkjrfan

    mkjrfan

    Joined:
    Aug 19, 2012
    Posts:
    38
    Hi, this is my first example tutorial so excuse me if this is the wrong place to put this.

    I've been having quite a bit of trouble finding a simple waypoint system that has consistent movement from one point to another. There are other alternatives like Vector3.lerp but they don't provide a set speed.

    With this example it includes rotating an objects Y axis to face its intended target and then move towards it with a constant speed.

    To set the way points place this code onto an object and then just place objects in the game screen that you want to set as your waypoint. Then drag those objects in the order you want into the this code's inspector.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class WayPointAi : MonoBehaviour {
    5.  
    6.     //This array is used to set which points are the waypoints
    7.     public GameObject[] wayPoints;
    8.  
    9.     //Speed and distance. Distance is used to determine how close you
    10.     //want the object to get before going to next point.
    11.     public float spd;
    12.     public float distance;
    13.  
    14.     //Holds the current waypoint it is going to
    15.     public int currentPoint;
    16.  
    17.     private bool change = false;
    18.  
    19.     //Holds the position of the waypoint it's heading towards.
    20.     private Vector3 targetPosition;
    21.  
    22.     // Use this for initialization
    23.     void Start () {
    24.         //Sets  the first waypointPosition
    25.         targetPosition = wayPoints[currentPoint].transform.position;
    26.     }
    27.    
    28.     // Update is called once per frame
    29.     void Update () {
    30.         //Checks if the distance from the object to the way point is less than distance given
    31.         //Once true it will increment to the next point.
    32.         if (Vector3.Distance(transform.position, targetPosition) < distance  !change)
    33.         {
    34.             nextPoint();
    35.         }
    36.         else
    37.         {
    38.             float dir = 0;
    39.  
    40.             //Moving and turning
    41.  
    42.             //Gets the direction to face
    43.             dir = -Mathf.Atan2(transform.position.z - targetPosition.z, transform.position.x - targetPosition.x) - 90 * Mathf.Deg2Rad;
    44.             //Draws the line to show where the object is going.
    45.             Debug.DrawRay(transform.position, new Vector3(Mathf.Sin(dir), 0, Mathf.Cos(dir)) * 20);
    46.             //Rotates the object towards to the waypoint.
    47.             transform.rotation = Quaternion.AngleAxis(dir * Mathf.Rad2Deg, Vector3.up);
    48.             //Moves the object forward
    49.             transform.Translate(Vector3.forward * spd * Time.deltaTime);
    50.  
    51.             change = false;
    52.         }
    53.     }
    54.  
    55.     private void nextPoint()
    56.     {
    57.         //Checks to see if it reached the max waypoint possible
    58.         if (currentPoint >= wayPoints.Length - 1)
    59.             //If true sets it to 0 so array doesn't go out of bounds.
    60.             currentPoint = 0;
    61.         else
    62.             currentPoint++;
    63.         //Sets the the targetPosition to the next waypoint.
    64.         targetPosition = wayPoints[currentPoint].transform.position;
    65.         change = true;
    66.  
    67.     }
    68. }
    69.  
     
  2. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    This helped me a lot.

    I changed the array of objects to a list of raycast hit points and a couple of other alterations and it works perfect for my situation.
     
  3. mkjrfan

    mkjrfan

    Joined:
    Aug 19, 2012
    Posts:
    38
    Thanks for using it glad it helps :)
     
  4. zfz.zahid

    zfz.zahid

    Joined:
    Apr 21, 2014
    Posts:
    2
    Thank you mkjrfan. Using your code I have solved my way points issue. I am sharing the the tweaked code.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoveWayPoint : MonoBehaviour {
    5.    
    6.     //This array is used to set which points are the waypoints
    7.     public GameObject[] wayPoints;
    8.    
    9.     //Speed and distance. Distance is used to determine how close you
    10.     //want the object to get before going to next point.
    11.     public float spd;
    12.     public float distance;
    13.    
    14.     //Holds the current waypoint it is going to
    15.     public int currentPoint;
    16.    
    17.     //Holds the position of the waypoint it's heading towards.
    18.     private Vector3 targetPosition;
    19.    
    20.     // Use this for initialization
    21.     void Start () {
    22.         //Sets  the first waypointPosition
    23.         targetPosition = wayPoints[currentPoint].transform.position;
    24.         Debug.Log ("Current Position = " + currentPoint + " Distance = " + Vector3.Distance (transform.position, targetPosition));
    25.     }
    26.    
    27.     // Update is called once per frame
    28.     void Update () {
    29.         //Checks if the distance from the object to the way point is less than distance given
    30.         //Once true it will increment to the next point.
    31.         if ((Vector3.Distance(transform.position, targetPosition)  < distance))
    32.         {
    33.             nextPoint();
    34.         }
    35.         else
    36.         {
    37.             float dir = 0;
    38.             //Moving and turning
    39.             this.transform.position = Vector3.MoveTowards(this.transform.position, targetPosition, 0.05F);
    40.  
    41.         }
    42.     }
    43.    
    44.     private void nextPoint()
    45.     {
    46.         //Checks to see if it reached the max waypoint possible
    47.         if (currentPoint >= wayPoints.Length - 1)
    48.             //If true sets it to 0 so array doesn't go out of bounds.
    49.             currentPoint = 0;
    50.         else
    51.             currentPoint++;
    52.         //Sets the the targetPosition to the next waypoint.
    53.         targetPosition = wayPoints[currentPoint].transform.position;
    54.         Debug.Log ("Current Position = " + currentPoint + " Distance = " + Vector3.Distance (transform.position, targetPosition));
    55.        
    56.        
    57.     }
    58. }
    59.  
     
  5. dasfuhrer

    dasfuhrer

    Joined:
    Apr 29, 2017
    Posts:
    11
    how to set the turning speed how to lerp between the curent rotation and the next rotation