Friday, April 24, 2015

"Succulent" technical overview / behind the scenes

This is a high level discussion of how I achieved certain effects in Succulent using Unity. It spoils the game, so I recommend you play it and/or read my artist's statement.

To the game engine, the popsicle (or "ice lolly" or corn dog) in Succulent is the main director for the entire scene. It is essentially a psychic telekinetic popsicle that dictates music playback, effects, and character animations... The popsicle is god. Love the popsicle.

To many developers, the most obvious straightforward way to achieve this popsicle-sucking interaction would've been to create a hand / arm controller, and then parent the popsicle to the dude's hand. But this "direct" way would've been the wrong way; this game is about popsicles, not about hands. Tuning the hand and arm movements necessary to pilot it into his mouth -- it would've been painful and unnecessary. (This is why it's important to have a fairly solid concept before you start coding something. The concept and design will affect how you code it!)

So I do it the "reverse" way: the popsicle is actually floating and rotating of its own accord, but it tells the hand to keep trying to grasp it. The hand and arm are totally cosmetic. Because this is kind of the reverse way of how we conceptualize motion, this type of animation technique is called "inverse kinematics." Instead of manually moving the arm, we tell the arm to "solve" for a "target" position, and it resolves the most "natural" pose based on joint hinge constraints.

If you're a Unity developer working with humanoid characters, make ample use of SetLookAtPosition and SetIKPosition (my code is based off the code sample on that page) and you can just move and rotate a target transform to pose your head / hands / feet, adjust these transforms while in play mode so you can get a sense of how the IK is solving, then copy and paste the transform data outside of play mode to commit your changes. You could technically animate an entire body like this. Very convenient.

I generally prefer using IK vs. canned Maya animations. IK lets me iterate pretty quickly with different poses, and then I can use code to make the IK responsive to game state changes. To do this without IK, you'd have to setup a Mecanim Animation Controller state machine and deal with a mess of variables and blend trees, it's kind of annoying and overkill for most of my uses.

This is a key benefit of being a generalist developer -- you can pick your implementations based on what you want to do. Being able to choose from a physics-based ragdoll animation, an old-fashioned hard-coded bone transform script, a Mecanim-based IK controller, or plain Maya keyframed animation, is really helpful. It's even better when you mix all those approaches together, capitalizing on the individual strengths of each approach.

For instance, the jaw control and "cheek physics" code is just some simple transform stuff. Based on the popsicle's rotation and position, I move the bone's localPosition. When tuned properly, it looks like the popsicle is causing his cheeks to bulge out.

Also notice the dude does not actually have teeth, partly because (a) you never really see the teeth in-game and (b) I was too lazy to model them, but also because (c)... teeth hurt.