This is the culmination of just under two weeks of development time.
The “Target Parent” and Unity’s IK
The first major headache was how to get the character’s hands to move. I knew from the beginning that I would use Unity’s IK system but not sure how to use it. To get the hands to move, I would need targets for those hands to follow.
Before getting into that, I first consulted the official unity documentation on ik along with this live training video and from there I figured out how to use animation controllers, clean up animations and use Unity’s IK system. I then came up with a solution to moving the character’s hands:
The target parent itself has very little code running behind it; simply rotating the actor according to right stick input. With the child targets for each hand, this allows for easy rotation of the hand targets around a fixed point on the character.
Bugs on the Way
Early iterations of the controller caused the sword to snap to wherever the stick was pointing, while that made controls super responsive, it had no weight. In response, I learned about linear interpolation and applied that to sword movement. This smoothed out the animation and made the whole thing feel a little more stable.
Behind the Back
As the rotations were calculated from the raw input of the gameplad with no changes. This allowed players to rotate their arms to extreme angles (namely, behind the back). I then discovered that you can clamp values in Unity’s scripting api. This, when applied to TargetParent rotation calculations, allowed me to set where to limit rotation to prevent angles that were too extreme.
Both of these fixes (along with a few other things) have public floats assigned to them, allowing for quick tweaks in the inspector:
Moving the Character
I wanted to have the character interpolate between animations realistically too. Much like Unreal Engine, Unity has a state-machine driven animation system which also supports 2D blendspaces.
Using the root-transform data contained in the animation clips, I am able to move the character using the animations rather than moving a Rigidbody actor. This allows for a character with a bit of weight when changing directions. However, it can feel a little clunky at times as some animation blends are a little messy and may need further refinement. Also I have not yet implemented backwards diagonal movement.
A relatively simple setup, it interpolates to the player’s position (with a tweakable offset) while it rotates to the player’s target. This means the enemy is always in focus along with player character.
While this is a solid foundation of what the gameplay is, there is still much to do:
- Grip modifiers
- Sword model
- Sword collision (physics)
- Character collision boxes (per limb)
- Dashing/sprinting (maybe)
and there is still the whole matter of local multiplayer, but that won’t be touched until the player controller is finished.