Unity Actor Framework ===================== Controllers and Presenters -------------------------- Unity already introduced the concept of the CharacterController, a component that lets the player control a character by keyboard, mouse and/or joystick. This framework groups all interactive GameObjects under the term "actors." All Actors are expected to have two components: an ActorController and an ActorPresenter - the controller handles the movement while the presenter deals with the cosmetic side. To understand the advantage of this design, let's take a look at the contents of the ActorPresenter class, which serves as the base class for all presenters: class ActorPresenter : ScriptComponent { public float Speed; public virtual void SetIdleState(); } When you create a new type of actor, you would derive your own class (let's call it "ScarySpiderPresenter") from the ActorPresenter and implement the SetIdleState(), SetLocomotionState() etc. methods to cause the matching animations to be played - for example by setting a parameter in your Mecanim state machine or by blending the appropriate animation with Unity's legacy animation system. Thanks to the ActorPresenter base class, you can now write controllers that play specific animations (waiting, walking, jumping, falling or hurt) on a model without knowing anything about what its animation state machine looks like - or if it has one at all: +----------------------+ +-----------------------------+ | ActorPresenter |<---+---| StationaryEnemyController | +----------------------+ | +-----------------------------+ /_\ | | | +-----------------------------+ +----------------------+ +---| WallCrawlingEnemyController | | ScarySpiderPresenter | | +-----------------------------+ +----------------------+ | | +-----------------------------+ +---| FlyingEnemyController | +-----------------------------+ This is the first step towards the reuse of enemy behaviors and writing controllers that can be mixed and matched with different character models. This has several uses, from writing extendable behaviors (letting you have a shared base class for enemies moving along fixed paths, for example) to scripted animations where characters follow predefined instructions. Conductors ---------- Putting all that control and AI code into a single controller class gets unwieldy very quickly. A player can be in many different states (for example, crouching is not possible during a jump, moving upright is faster than moving crouched and uses different collision boundaries, etc.). Sometimes a character may not even be controlled by user input at all. To split these states up into manageable chunks that can be combined and reused across different actors, the actor framework uses "conductors." For AI-controlled actors, each conductor is a behavior: pursuit, attack, escape and so on. Again, there are default implementations of these behaviors which you can use, extend or override with your own. For user-controlled actors, each conductor is a feat the actor can do: jump, walk, roll, duck and so on. The actor framework provides a default implementation of those feats which you can override with a custom one if you wish. Of course, you can define entirely new conductors as well. For example, you might want to override the jump conductor so that, if the jump key is pressed a second time during the jump, a jetpack activates (which then uses the entirely new JetpackConductor): +----------------------+ | ActorController | +----------------------+ /_\ | +----------------------+ +-----------------------------+ | PlayerController |---+--->| LocomotionConductor | +----------------------+ | +-----------------------------+ /_\ | | | +-----------------------------+ | +--->+ JumpConductor | | +-----------------------------+ | /_\ | | +----------------------+ +-----------------------------+ | MyPlayerController |------->| MyJumpConductor | +----------------------+ +-----------------------------+ | V +-----------------------------+ + JetpackConductor | +-----------------------------+