Skip to main content

C# Unity: AI Loop Waypoint System

For a looped waypoint system in Unity, attach this C# script to a game object which you want to move.

Enter a number in 'size' under Points. This is the number of waypoints which you want the car to hit. The last waypoint does NOT need to be the first waypoint.




In Unity, set up waypoint game objects with a collider, with 'is trigger' checked (like below).





Script:

public List points = new List();
public GameObject currentPoint;
public GameObject nextpoint;
int current = 0;
float speed, defaultSpeed;


void Start () {
   currentPoint = points[current];
   nextpoint = points[current +1];
   defaultSpeed = 4.3f;
   speed = defaultSpeed;
}

void Update () {
   transform.position = Vector3.MoveTowards(transform.position, nextpoint.transform.position, speed * Time.deltaTime);

   if(current < points.Count)
   {
      currentPoint = points[current];
      nextpoint = points[current + 1];
   }
}

Popular posts from this blog

VB: Countdown

About: A programme to countdown an amount of seconds, and display a message when the countdown has finished. Controls: 3  Labels 2  TextBoxes 1  Button Format:   (see image) Code:

VB: Alarm Clock

About: A program to replicate an alarm clock. The user enters a time, and the programme will display a MessageBox when the alarm hits. Controls: 4  Labels 1  Textbox 1  Button Format:  (See image) Code: