Last night I was rewatching one of my favorite movies (Heat), and decided to spend a couple of hours messing around with Unity – I’m a big fan of Unity, but haven’t had a chance to do anything with it since 3.0 dropped.
I wanted a quick project that I could finish before the movie ended, so I decided to throw together a simple ‘game’ that shows a few basic AI behaviors that are frequently used in many different types of games. Have you ever wanted to create some basic NPC AI for a game, but didn’t know where to start? Hopefully this project will give you some ideas.
There are three basic behaviors implemented here: follow, patrol, and wander:
- Right from the start there should be a soldier following you around.
- Head up the road to the left and check out some patrol behaviors.
- Head up the road to the right and check out some wander behaviors.
Simple! Click the image below to see the project in action.
Download the full Unity project by clicking here.
My hope is that this project will be of use to someone interested in getting started with creating NPCs in games. There’s plenty of room for improvement with the behaviors included here, but I feel like they’re pretty decent starting points for a beginner. If you open up the project, most everything should be easy enough to figure out. If the code seems jacked up, remember I wrote this while halfway paying attention to Heat – cut me some slack! Feel free to drop me a line if you have any questions.
Here are a few additional exercises for the reader if you’re the type who goes for extra credit:
- There’s currently no sort of pathfinding or navigation going on, but the NPCs do pretty well without it. One major exception is the follow behavior, if you try hard enough you can get the soldier following you to walk through the geometry of the level. Is there anything straightforward you can do to help this problem?
- Right now soldiers who pause to aim their guns are not actually aiming at anything, just pointing straight ahead. How about extending this behavior to actually point at something? Maybe other NPCs nearby or some geometry or something?
- On a related note, the soldiers do not currently use headlook – and it might be kind of cool if they did. There’s a sample headlook controller in the Unity Asset Store, and also on the web here.
- The movement system isn’t very well done. Soldiers don’t actually turn at all, they just snap their rotation to face the next point they’re headed to. This can be improved in a bunch of different ways…
- Are there any cool ways to combine these behaviors? I’ll bet you can get a halfway decent patrol formation going on by combining the patrol and follow behaviors, and making a few updates.
- None of these guys attack! You could always make them fire at you, if you’re not into peaceful soldiers.
Feel free to use the included behavior code as the basis for your own project, or to do whatever else you want with it. The visual assets used are from Unity’s Bootcamp demo or free downloads from the Unity Asset Store.
Check out a thread about this project on the Unity forums here.