There’s a lot of information about this, but I couldn’t find where to start on this problem. I’ve come to the point where I need to script the AI. I’ve scripted AI before, but with older engines and I’ve never used Pathfinding. So I need to program zombies, that have several targets. The main target is not the player, but a device (it’s like a tower defence, only in first person), but if the player is closer to the zombie than the main target, than chase and attack him until he get’s too far, than switch back to the main target. Also the player can throw bait, than if the bait falls somewhere arround the zombie, forget everything else and go eat the bait. Now, I kinda know how to do all this, the problem is I don’t know how to do all this with pathfinding. For example if I have a level with two floors and the main target is on the second floor. The zombie needs to find the stairs, climb them and destroy the target. I downloaded that Behave system, where you create collections and trees and stuff, but couldn’t figure out how to use it. I couldn’t access it’s documentation. I don’t even know if this is the right way to do it. So can you point me where to look for start ?
Hey–
I’ve been doing some similar stuff.
What I did is I raycasted to the various targets, and determined which one is closer. I also attached a radar script, so each target knew how many players were after it, so the A.I. could say “Hey, we don’t need 5 people after the same thing, lets go after different targets”. That was simple-- just have the AI increment a variable that is attached to the target, and compare all the objects in the vicinity.
For movement I did a line cast to each node (which was just an empty game object which fell until it hit something for easy placement, with a tag called “node”), used Djikstra’s algorithm (not A* since I’ve never used it, but I’ve read it is faster-- but Djikstra offers a way for the person to search for you)-- if it was possible to get there, the line cast would not collide with anything-- I also used 3 raycasts ot determine if the zombie could see the player-- if the zombie could see 2 of the ray casts, the player was visable (you need considerations for ducking, or a rail in front of the player). You also need to consider making a cone of visibility.
Each node kep a pointer to each node it could see, which was also done using line casts in Awake.
I would suggest you could use a similar approach, possibly tying in priorities to the various zombie targets.
A few considerations to think about-- how does the zombie turn after moving from one node to another?
Mine was simple, and I used ‘lookAt’ and ‘move forward’ with a character controller attached, but it hardly looked realistic.
I was working on a zombie game in the past, and wrote my code reusable as possible, but I really wouldn’t recommend it since it doesn’t work to well-- I quit half way through and started another project.
Good luck!
Hey thanks for the suggestions! I got several questions though. How did you linecast to all of the nodes? Did you use a for loop or another method? And how did you compare them? Also how does each node keep track of the next visible node, does it do it automatically, or you’ve manually set coordinates for the next node?
I used GameObject.Find(“Node”) to gather all the nodes into a large array. I then determined which ones to use using Physics.Linecast from the current object, to each one in the Nodes array. If it was visible, I stored it, if not, I ignored it. This was all done in awake.
Again, each node was a game object so I used the transform.position as its position (that way you can make a prefab and drag and drop).
After that, my walkable path finder iterated through the nodes (search wikipedia for djikstra’s algorithm) and created a shortest distance graph. My zombie then determined which ones to use based on location from that.
Just a quick note: the Behave library doesn’t, as far as I know, have any path finding capabilities. You would use that to build your decision logic for what to target / how to act, but you’ll need something else to do the actual path finding. Two very good path finding libraries for Unity are Angry Ant’s and Aron Greenburg(?)'s. Both have good documentation and examples, and are very capable.
Thanks both of you! May I ask what does actually a pathfinding library do?
ALright. So I downloaded Angry Ant’s Path, but look what the console said when I unpacked the unitypackage:
It tells you how to get from point A to point B. If your map has no obstacles, that’s a straight line, but as soon as you introduce fences, houses, lakes, etc. an efficient pathfinding algorithm is no longer trivial.
I understand. And is there a working pathfinding library for Unity 3.0, because the one I found gives the errors I posted above.
You might also check out:
http://forum.unity3d.com/threads/52326-A*-Pathfinding-Project-2.8-beta-is-live!
Thanks for the link, but this also doesn’t work. The error log is even bigger:
Could this be related with the version of Unity I’m using ? I just unpacked the unitypackage and did the first step in the documentation, which states:
Has anyone had this problem before with a pathifinding library? Whatever I do, the libraries just seem corrupt or missing something.