Introduction
The following is the character design on how to handle locomotion(3D movement such as walking,running and jumping), handling attack and on hit handler
Player Input Handler
I separate the input handlers as their own gameobject. The scene starts with the input handler reading the player input. If there are any other game objects they can refer to the following code to retrieve input.
Player Component
I break the operation into a smaller component. Each component handles a specific requirement. Example movement handler player locomotion, rotation and gravity.
- Player Animation
- Player Locomotion
- Player Hurtbox Handler.
- Player Audio
- Player Camera Handler
- Player Handler
Player animation is separated into 3 layers. Base layer is the player's main movement blend animation. Attack layer handle knife attack, aiming gun and fire weapon On hit layer handle when the player character got hit.
Handling Player locomotions and gravity
To to handle if the player been inflicted damage
Hold all player audio. Example if a player runs on a wood surface. It will create a wooden step sound.
Handle player camera, if the player is aiming or sprinting the attribute will be change accordingly
Act as the main brain of the character. It will initial all other component requirement
Player Locomotion
The following is the explanation on how character movement was handled.
After receiving player input. In PlayerHandler will submit 2 attributes, relativeMovement and relativeRotation. Relative movement is the direction of player input based on camera position while the relative rotation is rotation facing that direction. The return value of turnamount and forwardamount is for animation purposes. To indicate player is moving forward and rotation
- Movement Handler
- Gravity Handler
- Rotation Handler
- Player Inventory
- Jump Handler
- Collider shift
After receive direction and rotation, this function will add force to player to the direction of requested
If the character is not grounded, it will add downward force to the player
Similar to the movement handler, after receiving the relativeRotation. The player will be rotating to face the direction
Handle on Player weapons inventory.
As the name imply, this to allow player to jump
Similar to Dark Souls principle, collider shift is to provide a collider for the player to prevent passing through other objects. It shifted up a few units upward allowing movement in uneven surfaces such as stairs.
I was able to implement this by activating Auto Simulation, calculated the ground height and using Unity Character Controller.
Hurtbox and Hitbox setup
Hurtbox
The hurtbox bind to the character model. The hurtbox layer set to Hurtbox layer mask
Under the project setting, the physics setup is set as follows. This is able to simplify the hitbox and hurtbox calculation/interaction.
It handles when the character has been hit, this includes how much damage is inflicted, updating player status and which animation needs to be played.
Attack Handling
For attacking process, I use a ScriptableObject able to create a custom attack properties and in future able to reuse this model again
- PlayerAttackHandler is a component that handle player attack request.
- PlayerInventory will hold current player weapons. When the PlayerAttackHandler request to attack, it will pass the weapon reference
- PlayerWeapon hold the hitbox and particle(Blur and weapon trail).
- The weapon itself have custom weapon attribute.The reason behind this, is to provide flexibility on each attack value. Each variable for player action(ground attack,light attack,heavy attack and etc).
- SingleAttackAttribute hold cooldown timer and number of movelist.Example how many light attack move there is in this weapon
- Lastly, this class how much this per move will cost damage and if there any special value related to this move(knockback or launch).
Hitbox Handler
Simple explanation on how hitbox work.During the attack frame, it has an animation event request for knife collider to be active. When the collider is active it will check if it contacts any collider under Hurtbox physical layer mask. If it hit it will trigger the attack process and send information(damage,location,direction) to the enemy. Thus enemies will play animation based on the direction/location and update their health. On later frame there is another animation event where the weapon hitbox will turn off.
Example few animation events as follow. This allows a better attack mechanic when the player is attacking.
- Apply/RevertMovement - To allow or prevent player movement. Therefore when the player attack, it will disable the player locomotion
- Apply/RevertSwordParticle - To allow sword particle to be played or stoped
- Apply/RevertGravity - To modify play vertical movement on certain attack
- ApplyAttackRotation - Autocorrect attack to prevent missing the target on the frame of attack
- Apply/RevertHitbox - To able or disable weapon hitbox
- Apply/RevertRootmotion - To able or disable animator root motion
Player Animation Handler
The playerAnimation Handler is an extended class from UniversalAnimation. UniversalAnimation holds the animator component. The reason I created this component to allow me standardize the animation event function
In future project this class can keep extended to make fit any criteria
ExamplePlayer Layer Animation Handler
I created a custom state machine script. When it enters the state animation, the script will apply all the requests.
Example in the attack layer, when attacking the root motion will be activated. If there is no follow up request it will enter the ‘attack empty state’. This indicates the animation has been completed. While entering this state, if the script has a request for root motion and is disabled. The root motion will be disable
This function appears in another layer.(Movement Layer/Hurtbox Layer/Guard Layer). It is very useful and able to scale with other projects.
Player Camera Handler
Handling the player camera mostly using the Unity Cinemachine component. After receiving mouse input from the player input handler, this component will respond accordingly
Player Handler
I design Player Handler as the primary brain. All the necessary components are initialized in this component for standardized purposes.
Example above, the following how the player component is setup when initialize