Getting Started with 2D Inverse Kinematics
If you’ve read my previous blog post about the new 2D Animation package in Unity, you might be curious about what the new Inverse Kinematics package can add to your projects. Read on to find out about the basic ideas behind IK and how to set it up in Unity.
Getting the Package
As mentioned before, 2D IK ships as a separate editor package, and is available from the Package Manager – just like 2D Animation.
Basics of Inverse Kinematics
In order to understand Inverse Kinematics, we first need to be aware that they’re the opposite of Forward Kinematics – which refers to the typical process of manually moving and rotating each bone to arrive at the desired point in world space. The idea behind Inverse Kinematics is to take a chain of bone transforms and use an algorithm to move each transform so that the last bone is as close to a given target position as possible.
Inverse Kinematics are a handy tool in multiple animation scenarios: for example, they can be used to ensure that your character’s feet stay on top of the ground on an uneven surface; or to simulate a character grabbing onto an object. You could also use them when designing or prototyping your animation cycles to make bone positioning easier.
In this blog post, we’ll pick up where we left off with 2D Animation introduction, using the same rigged Viking sprite. To start using IK on our bone rig, we begin by adding an IK Manager 2D component to the highest object in the hierarchy – in our case, the rigged sprite Game Object which also contains the Sprite Skin component. Alternatively, you could attach it to the root bone of the rig. Once we have an IK manager, we can use the plus and minus buttons underneath to start adding and removing the different kinds of IK solvers applied to our object.
Adding IK Solvers
Before we move on, here’s a brief overview of the terminology that will be used in this blog post when talking about Inverse Kinematics, to make sure we are on the same page:
An IK chain refers to the group of bones parented to each other that will be affected by the IK algorithm. An example of a chain of bones could be an arm or leg of a character, however, it’s not limited to just limbs – the chain can span any number of bones, from the furthest bone to the root. For example, you could include the bones of the arm in a chain, as well as part of the character’s spine.
An effector is the end-bone or last child in an IK chain. The IK algorithm’s purpose is to bring the effector as close to the target as possible. An example of an effector could be the hand bone or a finger bone in a more detailed skeleton.
A target or goal refers to the target position in world space that we wish the effector to arrive at as a result of performing the algorithm. For example, if you wanted the character to grab hold of a weapon object, you would use its position as the goal.
We now need to make a decision about the most appropriate IK solver for our use case. An IK solver, in this case, simply refers to the algorithm that we use to rotate the bones in the IK chain, either until the effector is at the target position, or until we run out of iterations. There are many different IK algorithms, and you can, of course,, write custom ones yourself. By default, Unity’s IK package ships with three common types of solvers, which makes IK really easy to set up.
First, we have the CCD, or cyclic coordinate descent solver. This is one of the most popular IK algorithms for games. It works by iterating over all the bones in the chain one-by-one starting at the effector end of the chain, and performing a rotation on each one to be as close to the target as possible, until the effector arrives at the target or we’ve reached the maximum number of iterations allowed.
To set up a CCD solver, we need to first add it to the IK Manager component. After that, In the hierarchy, you will see that a “New CCDSolver2D” has been parented to our sprite object. We can now configure how it behaves and which bones it will affect. Let’s demonstrate how we can add IK to one of our viking’s arms. To do so, select the last bone in the arm chain, and in the hierarchy, create a new empty transform object (Right Click > Create Empty). The new object should automatically be parented to the bone. You can now grab this transform and move it to the tip of the hand bone. It will serve as both our target and effector.
Now, if we select our newly added CCD solver, there are several things we need to set up. First up, we have the option to assign our Target and Effector Game Objects. You can grab the empty Game Object you created earlier, and put it into the Target field. This will essentially serve as the last transform in our IK chain, and will rotate the tip of our character’s hand bone towards our goal.
The next step is to assign the desired chain length for our IK solver. Once you start dragging the slider, you will see yellow gizmos appear along the parts of the skeleton which will make up the chain. In this case, our chain length will be 4 – we have our hand tip transform, the hand bone, and the lower and upper arm bones. Once you’re happy with your chain, you can use the Create Effector button to automatically generate a transform for which our IK chain will be reaching.
And that’s it – you can now use the new IK gizmo that appeared at the viking’s hand to change the target, and the IK solution will react accordingly.
You will notice some other settings available in the CCD solver. They include:
Iterations – this setting determines the maximum number of iterations the algorithm will step through unless it is able to arrive at the goal earlier. We need to keep in mind that most solvers are iterative algorithms, which is why we need to set a limitation here for the case that the goal can never be reached. If you use IK at runtime, it might be worth keeping iterations quite low; however, if you are only using it for positioning in animations, you can pretty much increase the iteration count to be as high as you need to while recording your animations.
Tolerance – this determines what is the maximum tolerable distance that the effector can be from the goal for the IK algorithm to be considered complete.
Velocity determines the strength of the rotation performed on each iteration of the algorithm. Generally, higher Velocity values will mean that the goal is reached faster.
Finally, the Weight setting determines the overall strength of the applied IK solution, and is further influenced by the global Weight setting in the IK manager component.
If you don’t want the tip of the character’s hand to rotate with IK, and only want the wrist to be positioned at a specific point, you can instead use the hand bone in the Target field, and create the effector from that. Then either disable the Constrain Rotation checkbox to be able to position the bone manually, or rotate the effector instead.
The 2D IK package includes another type of IK solver which is particularly useful when animating humanoid characters, called the Limb solver. This is a modified version of the CCD algorithm which is limited to a chain of three transforms. It is perfect for positioning limbs, and also includes the option to ‘flip’ the IK result in case the joints in the limbs end up facing the wrong way; however, it does not include as many customization options as the included CCD algorithm.
We also have the FABRIK solver, which stands for forwards and backward reaching inverse kinematics. This algorithm first does a backward pass along the chain, starting with the effector at the goal; and then a forward pass from the highest bone in the hierarchy, applying the correct lengths to the points acquired during the backward pass. FABRIK generally takes fewer iterations to reach the target than CCD, but is slower per iteration if rotation constraints are applied to the chain.
If you wish to find out more about inverse kinematics and how the CCD algorithm works, you can check out a video on it in 3D context presented by James Bouckley at Unite Berlin 2018. James also explains his idea of modifying the original CCD algorithm.
As mentioned earlier, you are able to write custom 2D solvers and build on top of the existing ones in the package. For more details on doing so, please refer to the preview documentation available on GitHub. It will give you a good idea on how to get started!
It’s also worth noting that in the case that you have two different solvers affecting one or more of the same bones, the IK priority is determined by the order you have the solvers arranged in within the IK Manager, with items nearer to the top of the list having the highest priority.
Additionally, it’s important to keep in mind that even if you remove the IK solvers, the default position is not returned to the object unless you’ve had them keyframed in an animation, so it is a form of destructive editing. In cases where you wish to undo the effects of the IK solution, we include the option to Restore Default Pose to restore the bones to the point before IK was applied. Once that’s done, you can remove the IK solver safely.
Using IK in Animations
If you wish to use IK within animation clips, the positions of the IK effectors will also get added to the animation as keyframes when you change them – provided the effectors are children of the main GameObject being animated. You could add keyframes for all of your IK effectors at the start, but if you wanted to later remove them – you would need to keyframe the bones changed by the effector, and only then remove the IK. This way you will still keep the transforms of your bones in the right place; but keep in mind that the bones might arrive at the transforms differently since there is no longer an IK solution applied at every animation frame (unless you add keys every step of the way).
And that’s that! You’re now fully ready to use Inverse Kinematics with your animations. It’s worth noting that the tool is usable beyond the scope of 2D Animation, though the features definitely tie in well together. As always, you are free to experiment and find your own creative ways to utilize IK. Here’s an example of what you could end up with if you try attaching colliders onto the IK effectors:
Further Resources and Feedback
There is additional information on working with IK on the 2D IK repository, which contains the most updated documentation.
The documentation is particularly worth a look if you wish to find out more about the API and how to write your own IK Solvers.
On the same repository, you will find several sample projects that you can play around with which demonstrate the basics of the workflow described in this blog post.
We would love to see what you are creating with our new 2D packages, and to hear about your experience using them!
That’s one of the reasons we launched the Unity 2D Challenge. Create a small piece of content using some of our new 2D tools and you can win cash prizes and tickets to Unite! It can be anything from some pixel-perfect art to a thin vertical slice of a 2D game. The challenge is open for submissions until December 17, so don’t miss the deadline and get started .
You can also share your thoughts and projects with us on the 2D Animation Forum!
7 CommentsSubscribe to comments
Comments are closed.