Using the out of box AI Character Controller03 Oct 2016
I have been thinking about making a game lately that features an AI robot character. But this morning I had the idea to reverse the roles.
Long story short here is my experience using the AI Controller in Unity. Have a quick look at the finished product here.
The drone follows me uses the
AICharacterControl.cs class provided in the Unity Standard Assets. And it’s pretty easy to get it set up.
AIThirdPersonController.prefab into your scene. I swapped out the Ethan model for a free drone asset that I found.
Piece of cake right? All we do is add a target for our AI to follow or move towards.
Not quite. I got the error
"GetRemainingDistance" can only be called on an active agent that has been placed on a NavMesh.
Don’t know what a NavMesh is? I certainly didn’t.
A NavMesh is the approximation of walkable surfaces of a level. Not so important for a player, but incredibly useful for an AI to help it from falling down holes or getting stuck. They will look something like this…
Luckily Unity has got a pretty easy to use tool to help you generate this based on any gameobjects you have in your scene.
In the Docs there are some instructions that got me up and running pretty quick.
For consistency set up your NavMesh Agent’s radius, height and base offset before you start on the NavMesh. Make sure you use those values in the tool when get to baking the mesh.
I had it easy in that a drone can pretty much go anywhere, you might need to be stricter depending on your character.
Your AI character should now be moving around all on its own.
I spent a good half hour tweaking the steering settings till I found something I was happy with.
Now it is down to you to take it as far as you need. Unity NavMeshes use A* to calculate the shortest path. It also allows you to modify the NavMesh with different types of layer masks and apply costs to each mask. This is how you would make your AI avoid the water if it would slow them down.
Pretty damn cool stuff :D