Starter controller: How to use ?
After months of struggling, I have finally designed a controller structure that, in my humble opinion and for beginners projects, is very well suited. To do so, I rely heavily on animation state machines, which allow to communicate with the animator, hence keeping track of what state the player is actually in. It may sound very trivial, but this is crucial and has hindered my small games for a long time in the past.
I’m making a small guide on how to use to help interested people gain some time and also as a reminder to me, in order to prevent forgetting valuable informations.
Basically, the structure relies the following elements:
Modular: The central hub
- StateBehav folder: Holds various scripts for handling animation states
Move: Manages the player’s movement
Fight: Manage fighting states
The information pipeline is as such:
- Player’s input is recorded by the
- This information is accessed by the
Modularscript in the
- The relevant script is called (usually
- Most of the time, a trigger for animation is set to true (ex: hit, jump…)
- Most of the animation states hold a
InformModularscript with an Information field. This is filled by the user.
- When the animation state is accessed, the
InformModularsends the information to
Modularalong with a boolean set to true when entering the state, and false when exiting.
- This state and information are used by
Modularto update the character state. Usually, a specific method is set up to comply with needs (eg: OnJumpEnter will set rigidbody’s drag to minimal value, apply the force and enable the additional gravity)
I’ve found the method to allow for easy extention and it handles rather well various character states.
The hitbox magic
The wiring necessary for hitbox wasn’t totally straightforward, so here’s an explanation.
- It relies on two main components:
- The Hitbox script, to be attached to triggers
- HitData data struct. Holds informations about the hit
- First, equip the hit animation states with a
- This script has a HitData field. In here, various informations have to be filled.
- When the state is entered, the
Modular.HitInform()method is called, with the HitData as parameters
- From here, the
Fight.Activation()method is called. It sets the hitbox with corresponding name to an Active state. In this state, it will indeed generate an impact when touching something.
- When the state is exited, the same method is called, but this time it sets the hitbox to false.
The other infrmations in HitData are used for applying an impulsion to the character, setting damage and force applied to enemy and so on. The HitData struct is located in the