How to pick random waypoints

Hey Guys,

Im following some tutorials online about waypoints. I have got something working where my Ai moves to the way points in order of the array. But Ive been trying to figure out how to many it RANDOMLY pick between way points…any ideas on how to do this any help would be really appreciated.

Heres the code:

var waypoint:Transform[ ];
var speed:float = 20;
private var currentWaypoint: int;

function Update ()
{
if (currentWaypoint < waypoint.length)
{
var target : Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;

var velocity = rigidbody.velocity;

if (moveDirection.magnitude < 1)
{
currentWaypoint.Random.Range++;
}

else
{
velocity = moveDirection.normalized * speed;
}
}

rigidbody.velocity = velocity;

}

Use Random.Range to obtain a random value between 0 and the size of your waypoints array.

var randomWayNumber : int = Random.Range(0,waypoint.Length);
var wayPointLocation : Vector3 = waypoint[randomWayNumber];

Note: Unrelated to your question but something to keep in mind is that Random.Range is non-inclusive for it’s max value when dealing with int types. This works for us when we are looking for 0 indexed numbers in an array where there is no element at array[length]… not so good when you’re trying to get a fixed range… If you want values between 1 and 10 for instance you’d specify Random.Range(0,11)