- Home /
Walk to random waypoint
My char. is walking along a set path of waypoints right now. I am trying to make him walk to a random waypoint instead. Anyone know of a solution? Perhaps assign a random.range index for the waypoints? I cant figure it out how to do it.
// ... increment the wayPointIndex.
if(wayPointIndex == patrolWayPoints.Length - 1)
wayPointIndex = 0;
else
wayPointIndex++;
// Reset the timer.
patrolTimer = 0;
}
}
else
// If not near a destination, reset the timer.
patrolTimer = 0;
// Set the destination to the patrolWayPoint.
nav.destination = patrolWayPoints[wayPointIndex].position;
}
Comment
Best Answer
Answer by HarshadK · Feb 18, 2015 at 08:07 AM
Replace:
if(wayPointIndex == patrolWayPoints.Length - 1)
wayPointIndex = 0;
else
wayPointIndex++;
with:
wayPointIndex = Random.Range(0, patrolWayPoints.Length);
Your answer
![](https://koobas.hobune.stream/wayback/20220613183638im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can't set currentWaypoint integer with Random.Range 3 Answers
Random.Range statement question 1 Answer
Array index is out of range and Raycast question 0 Answers
Various of Random Numbers? 1 Answer