With the release of Unity 5, the Unity team introduced their new mecanim system to the world. Mecanim is a system that allows users to create put together animations, create transitions and build complex and sophisticated animation state machines.
Although mecanim is aimed at animations, under the hood it is but a finite state machine with conditional transitions between states. We took advantage of this and create an animation controller without any animations!
In our game, our victims can have multiple behaviours:
- None
- Following the player
- Going to safety on their own and returning or panicking when they get lost
- On Fire
- Rogue
At first, the victims only had the first 3 behaviours, so it was fairly simple to manage within 1 Victim script using an enum and a switch statement. However, once we started expanding the behaviours victims could have, the codebase grew large and no one wanted to approach it anymore.
Mecanim to the rescue!
We took the AI of the victims, separated it into different StateMachineBehaviours, which are special components, extending from ScriptableObject, that Unity allows users to put on different states inside their mecanim system, and created a controller for the victims.
And every state in this state machine has a behaviour attached to it determining how the Victim behaves in that state
![](https://i.gyazo.com/219169f1f4fa65d483d81da6be3a7fcf.png)
Afterwards, instead of a huge mess inside the original Victim script, all that is left in there is one method that sets the Behaviour parameter in the state machine that determines to which state the victim transitions to.
![](https://i.gyazo.com/2aac6bbe0d29bc844ee7c8239223554e.png)
Moving to this system and utilizing the power of Unity's mecanim allowed us to easily extend and modify the functionality of the victims, making the game much more fun and enjoyable!
No comments:
Post a Comment