UPDATE: The last 2 major releases of Unity adversely affected how UnitySteer steering behaviours play with non-kinematic RigidBody components. If you’re having trouble combining your physics with steering forces, see this enhancement I added, which may or may not become a pull request at some point (although I think the UnitySteer project is not actively maintained any more).
There are several AI steering libraries available for Unity, but I settled on UnitySteer due its similarity to OpenSteer for C++.
Setting up UnitySteer examples
Before diving in and adding steering behaviours to your own projects, you may want to get the examples project working, in order to see what UnitySteer can do. The UnitySteer project is hosted on GitHub – here. You could just download the project and the examples project as ZIP files, but cloning them via Git is recommended. If you don’t have Git set up, start here.
Assuming you’ve set up Git and it’s in your path (setting up Git is beyond the scope of this post, there are plenty of tutorials out there), clone the UnitySteer project somewhere:
git clone --progress -v "https://github.com/ricardojmendez/UnitySteerExamples.git" "C:\Dev\Libs\UnitySteerExamples"
Then you’ll need to import the UnitySteer library itself, which is a submobule of the examples project:
git submodule update --init -- "Assets/UnitySteer"
Navigate to one of the folders in UnityExamples/Assets/Examples/ and pick a .unity scene to open and play around with.
If you’re using Unity 5, it may say it needs to upgrade the project; just agree and let Unity rework the script for you – this may take a while – the prompt looks something like this:
Adding UnitySteer behaviours
If you want to add steering behaviours to your own project, all you need do is copy the Assets/UnitySteer folder to your project’s Assets folder (if you do this while Unity is open, give it a minute to import it). Ideally, if your project is under Git version control, you’d just add UnitySteer as a submobule – but it’s not essential for getting started.
You can now add UnitySteer components to your game objects, just like other components. Select a game object and choose Add Component in the Inspector, click UnitySteer…
You will need to a type of vehicle to your game object before steering behaviours will do anything. Autonomous vehicle should suffice for most needs. Here I’ve added a pursuit behaviour to an object, making the player its quarry (so it will chase the player).
You can still have colliders, rigid bodies and such attached to a game object with UnitySteer components. However, if you wish to move a game object yourself, but still make it ‘findable’ by other UnitySteer controlled objects (for example, your player object), you should give that object a passive vehicle UnitySteer component.
You can add multiple steering behaviours to one object (or prefab) and use the weight property to balance the bias between behaviours. I created a simple example to demonstrate mixing of steering behaviours. The following demo mixes evasion, pursuit, follow and wander behaviours. Click inside the game window and use the arrow keys to move the player (blue box). The big red box chases the player, while the small red boxes wander a little, follow the big red box and evade the player.
You’ll need Unity Web Player and maybe FireFox or pre-Windows 10 Internet Explorer to open it. You can enable NPAPI plugins on Chrome, but they’ll be removing support for this soon: