What is the simplest way to make my AI soldiers walking on defined path? I would like to make simple path on which will be my soldiers walking on. For example around one building like a guards. (some kind of waypoints I guess…) Should they have rigidbody and ‘add force’ or anything else. I need only realy simple paths for them.
check this awesome pathfinding tool from sturestone:
http://forum.unity3d.com/threads/67417-A*-Pathfinding-2.9-Is-Released-(Unity-3-Compatible)?highlight=astar
It;s probably overkill for what you need, but it should give you some ideas even if you can;t use it directly.
AI can be a tricky wicket. At first, what might be simple turns into something not so. To answer your question, you can easily use a character controller, make it’s rotation face towards the waypoint, tell it to move in that direction and it will appear the way you want.
This is all very simple. What I would do is to have the character’s target be an invisible box (making sure to destroy the collider for it) and every frame just ask if you reached the point, if not, move it some more.
Concentrate on making your little guy move from point A to point B right off the bat. Point B is ALWAYS your invisible target. Beware on your code though, don’t just say, if(character.position == target.position) Always give some room for error. So: if((character.position - target.position).magnitude < 5.0) will produce a better result. (Do try to avoid putting waypoints too high. Sim’s cant jump that far)
The point of using a blank target is that, for one, it represents the last visible spot that the character should be moving towards. This means that if your soldier sees an enemy, he moves towards that spot, if he stops seeing the enemy, don’t just have him continue towards the enemy’s current position, have him to towards the last visible position. Then, at some point, if he see’s the enemy again, he simply retargets and starts moving in that direction. (Cool thing is, if he never sees the enemy again, you can have him stop right there for a certain amount of time and look, then return to his patrol after that)
OK, lets then discuss a WayPath. A series of WayPoints in order. You can define these as a single object with children. (I usually just create boxes in my scene and color code them to a path. Of course make them invisible and destroy the collider) This way I have a good representation of where my path is going.
For a WayPath, I would create a script defining the points, what order and what type of path it is. (Circular, PingPong or OneWay, you could also define this in the character. This is the path he follows and he PingPongs it would create a nice patrol. A Circular WayPath would be a race. )
OK, next part, which is alot theory. You can define each WayPoint as well. Each WayPoint can control various things. (Waiting, Splitting, Speed) So if your character should walk slowly accross a bridge or even stop in the middle and look around. He could also move faster in a long tunnel because there is nothing there. Splitting is where the character could (or should) stop walking on this WayPath and start walking on another.
All this is dumb AI though. knowing that a person turned another corner and is hiding right behind the wall is a bit more tricky. A* is a nice system for general path finding. I would suggest reading as much as you can on the subject.