Monday, October 10, 2011

Working with AI: Movement

To Start spawn an AI grunt unity by going to rollup bar > Entity > AI > Grunt. 






To choose a character scroll down to the model parameter




It is possible that you chose a character with no hands, if so, this is easy to fix. 




 Go to topbar > view > open view pane > character editor.




Go to file > open and open the character model that you have in your scene.  In the bottom left under attachments click add, and call this attachment hands.





In the section to the right of the attachments click on the … after the object, in the file window back out to /Human and then enter the folder Hands.  Find a hand or glove model that looks like it would match the arm position of the character.  





Then click apply.




You now have a character with hands




Save the character (file>save) and close the window.  Select your grunt again and change the model to the character you created.  (click root in the top left corner then follow the path Game > Levels > Name of your Project) 




Now that we have a character in game we want them to move.  One way that we can do this is by adding an AI path (rollup bar > AI > AIPath).
NOTE: if your crysis crashes at this point it is likely due to tablet services.  Open services.msc in windows and stop any tablet tasks (for instance I have to stop Tablet PC Input Service and TabletServicePen).




You can now draw a path by clicking to add points and double clicking to finish the path.  If you are unhappy with the path you can click edit shape to change the points as you would like (remember to select direction at the top in which you would like to move the point.




Once you are happy with your path select your grunt and scroll down to the flow graph section.  Click create to open the flow graph window.  Flow graphs are a powerful, yet daunting, tool that we can use to control objects and AI in our game.  Flow graphs are node based, which you can add by right clicking and selecting what type of node you would like to add.



When you do: Right click > Add node > misc > start it adds a node to the graph, unfortunately you are so far zoomed out that you will not be able to see this node, to fix this Right Click > Fit Graph To View.




Now we should add two more nodes.  First: Right click > add selected entity and second: Right click > add node > AI > AIFollowPathSpeedStance. 
If you look at your nodes you will notice that each node contains several values on the each side with arrows pointing both in and out of the node.  These values correspond to inputs and outputs, if you click/drag on an output arrow of a node you will notice that a blue arrow appears that you can move around, this allows you to direct the output from one node into the input of another node.
We want to have the following I/O in our graph Start>output to Grunt>Enable and Grunt>Enabled to AIFollowPathSpeedStance>sync.  If done correctly it should look like:





Now if you look closer at AIFollowPathSpeedStance you will notice that the top value is highlighted in read and reads ‘choose entity’ we need to assign it the entity that we would like to follow the path.  To do so make sure that the grunt is selected in your scene and rightclick on the value and click ‘assign selected entity’.
With the AIFollowPathSpeedStance selected you will notice the input window in the top right, this allows us to change the values that are used, we want to change ‘StartNearest’ to true and change ‘path_name’ to whatever the name of your AIPath is.  Once you have done so save your flowgraph and make sure it looks like this (with your path_name set correctly):




Now if we jump in game we would expect the grunt to walk along the path, but for some reason he does not.  We need to change one last value for the path to work.  Select your AIPath and in the entity parameters make sure the first value ‘Road’ is set to false.  Now if we jump in game the AI will walk along the path.
NOTE: if you are standing to close to the grunt he will react to you instead of walking along his path.




We can change how the grunt will move along the path by opening the flowgraph and changing the ‘run’ and ‘Stance’ input values on the AIFollowPathSpeedStance.  You can make the grunt stealth walk by selecting Stealth in the Stance input, or make the grunt run by setting run to 3 and stance to combat.





The second way to make grunts move is by use of a tagpoint and a goto, using this method you drag a point onto your map and the grunt will path find his way to the point.

To add a TagPoint go to rollup bar > AI > TagPoint and drag it onto your view window.




And place it in your map wherever you would like




When you are happy with the placement of your tagpoint select your grunt and create a flowgraph.  Add the following nodes : Misc > Start, AI > AIGoto, Entity > GetPos.  Drag arrows from Start>output to GetPos>Get and AIGoTo>sync and from GetPos>Pos to AIGoTo>pos.  Finally assign the grunt as the entity for the AIGoTo and the Tagpoint as the entity for the GetPos.  If done correctly your flowgraph should look like this:




After saving you should be able to jump into the game and see your AI unit walk to the point.  But what if we don’t want the grunt to walk through a certain area?  Well that is where ForbiddenAreas come into play.  To add a forbidden area go to rollupbar > AI > ForbiddenArea and draw an area between your grunt and your TagPoint much like you did with the AI Path (double click to end).  




ForbiddenAreas are regions that you designate that AI units are not allowed to go through, so if you run the game you will notice that your AI unit walks straight through your ForbiddenArea.  Every time you add a ForbiddenArea you have to recalculate the navigation so that the AI knows not to pass through the area.  To do so go to topbar > AI > Generate All Navigation, now you can drop in and your AI unit will walk around the ForbiddenArea.


No comments:

Post a Comment