- Home /
Help with a simple AI
Hey guys I have made a simple waypoint function but I am having a bit of trouble. What I want it to do is this:
locate closest waypoint, go to it, if it reaches it, search again without including the one its currently on.
As you can see in the code below: (distanceToWayPoints[closest] < totalDistance) that block of code has to be changed
void WayPoint() {
// total distance
int totalDistance = 10;
// goes through all the waypoints
for (int nStart = 0; nStart < MaxNoOfWPs; nStart++)
{
// calculates all the distances from the monster to each waypoint
distanceToWayPoints[nStart] = Vector3.Distance(everyWayPointInLevel[nStart].transform.position, transform.position);
// if its the closest waypoint than have it be the target
if (distanceToWayPoints[nStart] < distanceToWayPoints[closest])
{
closest = nStart;
target = everyWayPointInLevel[closest].transform.position;
//used as temp to know which one was the last wp
lastWayPoint = everyWayPointInLevel[closest].transform.position;
}
// if the enemy gets close enough to the waypoint, trigger this =
if (distanceToWayPoints[closest] < totalDistance)
{
// THIS HAS TO BE CHANGED
// so that instead of placing the wp at 0,0,0
// it makes it not calculate the wp in the search
everyWayPointInLevel[closest].transform.position = new Vector3(0,0,0);
}
}
// animation
animation.CrossFade("walk");
// speed
speed = walkingSpeed;
}
It seems like if it worked, it would just ping-pong between a small group of points. For example, if the waypoints were a rectangle, it would just walk back-and-forth on a short side.
True, but I have about 70 wps all together scattered around the level, I will just have to see when this is fixed
You have the right idea (I think you can lose the distanceToWayPoints array, then add the currentWP idea, from below.) BUT, try this: put your finger on the starting WP. $$anonymous$$ove to the closest. Then move to the closest again (counting the starting point.) It will take about 30 seconds to do that, and I think you'll see the problem. Then pretend you add an "oldWP" var, to avoid going backwards, and hand trace that quickly. I think you'll always get some sort of loop.
Answer by DaveA · May 21, 2011 at 12:42 AM
I would set a new variable 'current' to the current waypoint's index, and have your loop avoid checking it if (current != nStart) or something like that.
I think I have tried something like that already and it didn't work but I will try it again, maybe I was doing it wrong somehow.
Answer by Ppa0 · May 21, 2011 at 09:29 PM
I had to tweak it a bit, but adding the variable helped out a lot, Thank you both!
Your answer
Follow this Question
Related Questions
Incredibly high speed tweening/enemy movement 0 Answers
Enemy detection while pathfinding through the map 1 Answer
AI Pathfinding 1 Answer
Why isn't a Ai Waypoint Spawning??? 0 Answers
AI Pathfinding In A Certain Height 1 Answer