Creating a dynamically animated character walking on Spriteshape
Published 2 years ago
1.1 K
An ongoing project figuring out how to combine 2D Animation, 2D IK and SpriteShape together.
When Unity showed SpriteShape back on GDC2018, a question I had in mind was how would a character move on such an unpredictable surface. It's a given that a level designer would modify the surface any way it could, so there's a point where a prerendered character animation would look off moving on the platform. So I decided an experiment to see if it's possible to create a character that would dynamically place it's limbs correctly on the undulating surfaces of SpriteShape.
That means combining 2D Animation, 2D IK and SpriteShape into a single project.
Understanding 2D Animation and IK
First test was to see how 2D Animation with IK worked. That meant slicing up an image to see how it would be rigged and animated. For test purposes, I used an image of Arthur from Nanatsu no Taizai (because I liked that particular heroic pose).
The point of this test was to compare how a multi-mesh solution would work compared to single-mesh. The sample Unity provided was a single-mesh applied on the whole character, which meant there were deformities when the bones were angled to the extreme. A multi-mesh setup should work better, so I needed to study how the sprites should be prepared.
The Z-Fighting was easily solved by adding Order to the bones. This was done on April 15th, BTW. The recent Unite Singapore 2018 on November announced an update on how Unity would import the sprites by referencing the layers within Photoshop. I haven't tested it, but that should make layering and setting up the 2D characters easier.
Once I figured out that I can cut up sprites into parts to have bones attached to it, I know I can experiment by creating separate sprites in various shapes (which are not SpriteShapes), complete the bone rig, and then - ideally - replace the sprites later.

Dynamic Animation using Physical Forces
From what I gathered, 2D IK essentially has limbs that were moved by an effector. Logically if a physical object was attached to the end of the limb, it will respond to any physical forces. So letting it fall onto a surface would bend it like a limb. However, a limb ideally would try to retain a certain level of bent; when pushed to a close it should open when left alone, pulled straight and it should pull back to a certain angle when left alone.
So the each limb is given these setups to its effector: 1) A CircleCollider+Rigidbody2D to give it a physical property that can have forces applied to it. 2) A DistanceJoint2D+SprintJoint2D to push/pull the limb to a specific angle if no forces are applied. 3) For testing, a visible sprite - here, a golden "coin" to show how the feet contacts the surface.
Since around April 2018, the 2D Animation and SpriteShape implementation were in separate GitHub downloads, I couldn't test them working together. I tried, believe me. So until the implementations are released through the Package Manager, best I could do was to create a walking character on a flat surface. Here, movement forces were applied upon the main body, and when a limb goes too far, forces are applied to the effectors to make them look like they're moving ahead of the body.
Once 2D Animation and SpriteShape packages were released through the Package Manager, I was able to test the combination. There's still work needed in making the body correct its height according to the surfaces and placing the feet correctly, but the illusion seems to be working.
That's the update so far. Will add to this article as I continue building the project.
Hilmy Abdul Rahim
Experiments with Unity - Producer