Hello. I am having an issue with a waypoint-based AI system. I am trying to get it to have a branch in the path and then take the randomly chosen path, like this:
var waypoint : Transform[];
var speed : float = 25;
private var currentWaypoint : int;
private var loop : boolean = true;
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) {
if (waypoint[currentWaypoint].gameObject.CompareTag("WaypointBranch")) {
var branch : int = Random.Range(0,10);
Debug.Log (branch);
if (branch >= 5) {
currentWaypoint++;
}
else {
currentWaypoint += 2;
}
}
else {
currentWaypoint++;
}
}
else {
velocity = moveDirection.normalized * speed;
}
}
else {
if (loop == true){
currentWaypoint = 0;
}
}
rigidbody.velocity = velocity;
transform.LookAt(target);
}
I, as you see, do get a branched path, but then I’m not sure how to get it to pick up from the certain point on the path. I’ve tried a couple of ways to fix it but neither of them worked, which is why I decided to ask here. Any help I could get would be appreciated. Thanks!
Well yes, that would work. I was thinking more along the lines of a system that could be used no matter what, in Any instance. This would only split the paths to every 5 in the array. I want to make it so that it can go to a branch of say, 2, and still continue. I do have an idea, but I doubt it will work.
EDIT: It didnt work.
Okay. I have a new explanation of what I mean. Say the path is a tree. At the fork you could take one of two paths. These paths would go there own independent ways and could (or not) meet up later or branch off into more paths. I need a way to go from one waypoint to another with a very different index in a kind of procedural way.
I am trying to make this waypoint script from scratch, its how I like to do everything. I just need help.
Ok, well what about a 2D array of ints that indicate the next waypoint? You’d have to set up the array manually.
Since there is no branch at waypoint 1(based on your original drawing),
nextwaypoint[1,0] = 2
and
nextwaypoint[1,1] = 2
but waypoint 2 branches, so
nextwaypoint[2,0] = 3
and
nextwaypoint[2,1] = 4
and you could choose like this:
var branch : int = Random.Range(0,10);
if (branch >= 5) {
currentWaypoint=nextwaypoint[currentwaypoint,0];
}
else {
currentWaypoint=nextwaypoint[currentwaypoint,1];
}
Better yet, you could make a waypoint script and attach it to each waypoint object. It could hold an array or list of ints indicating nextwaypoints. You could reference the script and its variables with GameObject.GetComponent.
The idea with the 2d array idea is to kind of fix code the pathway with given choices (in this case, two choices). Where there is no branch, you have to choose between “the next waypoint” or “the next waypoint” (in the given example, waypoint 1 can only go to 2. So option one is 2, and option two is 2). However at points where you could go to different places, your choice is “that waypoint” or “another waypoint” (the given example, again, is from 2 you can go to 3 or 4). Then, depending on the randomness involved, you’d pick one of those different points.
So let’s say you’ve got a simple 4 waypoint system:
1 leads to 2
2 leads to 3 or 4
3 leads to 1
4 leads to 3
Thus there is only one real branch. Here’s how the array would look:
This is limited because you never really get a sense for the map as a whole, and you’re stuck with 2 branches from a single point.
A better alternative, I’ve found from past experience, is to use a linked list with a vector of pointers to other nodes. This way any given point can be the branch of a vast network. You can also maintain a mirror vector which stores the costs of those routes, meaning searching for a given path between two points becomes easier. You just have to be sure that you don’t recurse endlessly somehow, like with a unique node identifier. If you’re retracing your path, chances are you’re building the wrong path and your heuristics or weighting are wrong.