2D Inverse Kinematics (IK)
The 2D Inverse Kinematics (IK) package allows you to apply 2D IK to the bones and Transforms in your project. When you position a bone to a specific position, 2D IK calculates for the positions and rotations of the other bones connected to it in the chain. This makes it easier to pose bones for animation, or to manipulate a skeleton in real-time, as you not need to manually position every bone in the skeleton.
The following workflow ideally continues from the 2D Animation workflow, as it demonstrates how to implement 2D IK onto your skeletons.
Add the IK Manager 2D component to the GameObject at the top of the hierarchy.
Add to the IK Solvers list by selecting which type of IK Solver to use. Each addition to the list creates a new GameObject in the hierarchy.
With an IK Solver selected, select its Target bone/Transform (e.g. an arm or leg bone). Create or set an Effector for the IK Solver.
Pose the bones by changing their positions directly, or by using the Effectors.
When you move a Target bone to a new position, an IK Solver automatically calculates the rotation and position that other bones in the chain should take to achieve that goal position, removing the need to manually position them yourself. Each type of IK Solver has its own algorithm that makes them better suited to different kinds of conditions.
The following are properties which are common to currently available Solvers:
The IK Manager 2D component controls the IK Solvers in the hierarchy. When a Target bone/Transform is attached to an IK Solver, the IK Solver calculates the rotation and position that the other bones in the chain should take in order for the Target to reach its destination. This removes the need for you to manually position each bone in the chain.
Add the Manager component (INSERT PATH) to the highest bone in the hierarchy, commonly referred to as the Root bone.
A drop-down menu then appears with three options - Chain (CCD), Chain (FABRIK), and Limb. Each type of IK Solver uses a different algorithm for its solution.
IK Solvers are iterated in descending order, with Solvers lower in the list referring to the positions set by the higher Solvers. The order of Solvers usually reflects the order of bones/Transforms in the skeleton hierarchy. For example, if the arm bone is the child of the torso bone, then the torso's IK Solver should be set above the arm’s Solver in the list. Rearrange the Solvers by dragging the leftmost edge of a row up or down.
Weight measures the degree that a Solver’s solution affects the positions of the bones/Transforms in the chain. The IK Manager 2D has a master Weight property that affects all Solvers it controls, and is applied in addition to the Solver’s individual Weight settings.
Restore Default Pose
Click this to reset all bones and Transforms back to their original positions from before the 2D IK was applied
This is a standard two bone Solver that is ideal for posing joints such as arms and legs. This Solver’s chain length is fixed to three bones - the Target bone/Transform and two additional bones in its chain.
Chain (CCD) - Cyclic Coordinate Descent
This IK Solver uses the Cyclic Coordinate Descent algorithm. It’s solution becomes more accurate the more times its algorithm is run. The Solver stops running once the set tolerance or number of iterations is reached.
The following property is only available to the Chain (CCD) IK Solver:
|Velocity||The speed the IK algorithm is applied to the chain the Target bone reaches its destination.|
Chain (FABRIK) - Forward And Backward Reaching Inverse Kinematics
This IK Solver uses the Forward And Backward Reaching Inverse Kinematics (FABRIK) algorithm. It is similar to Chain (CCD) as its solution becomes more accurate the more times its algorithm is run. The Solver stops running once the set tolerance or number of iterations is reached.
The Chain (FABRIK) Solver generally takes less iterations to reach the Target's destination compared to Chain (CCD), but is slower per iteration if rotation limits are applied to the chain. This Solver is able to adapt quickly to if the bones are manipulated in real-time to different positions.
After creating an IK Solver, the next step is to set the Target bone and its Effector. An Effector is a Transform that represents the target position the Target bone tries to reach. As the Target bone moves towards the Effector’s position, the IK Solver calculates for the position and rotation the other bones in the chain.
Follow the steps below to set up an Effector:
Create an empty Transform (right-click > Create Empty). It is automatically created as a child of the highlighted bone.
If the Create Effector button appears inactive, ensure that the Chain Length value is set to one or greater.
- The Effector is created as a child of the IK Solver. It appears as a circle gizmo in the Scene view. Move the Effector to manipulate the connected chain of bones managed.
Scripting API Reference
Adding New Solvers
You can add your own solver by extending from the class Solver2D. Your extended class will then show up as a new solver under the solver menu in the IKManager2D component.
This is the base class for all IK Solvers in this package. IKManager2D will detect all classes extending this and accept it as a Solver it can control. Implement or override the following methods to create your own IK Solver:
- protected abstract int GetChainCount()
This function returns the number of IK chains the solver owns. Use this to return the number of IK chains your solver owns.
- public abstract IKChain2D GetChain(int index)
This function returns the IKChain2D at the given index. Use this to return the IKChain2D your solver owns at the given index.
- protected virtual bool DoValidate()
This function does validation for all parameters passed into the solver. Use this to check if your solver is set up correctly with all inputs.
- protected virtual void DoInitialize()
This function initializes the solver and builds the IK chains owned by the solver. This is called whenever the solver is invalid after changing the target of the solver or other parameters of the solver. Use this to do initialize all the data from the parameters given to the solver, such as the IK chains owned by the solver.
- protected virtual void DoPrepare()
This function prepares and converts the information of the Transforms (position, rotation, IK parameters etc) to structures which can be used by the IK algorithms. Use this to do any work to gather data used by your solver when updating the IK positions.
- protected abstract void DoUpdateIK(List effectorPositions)
This function calculates and sets the desired IK positions for the Transforms controlled by the solver given a list of effector positions for each chain owned by the solver. The effector positions may be overridden by user positions if manipulated from the SceneView.
- protected virtual Transform GetPlaneRootTransform()
This function returns the transform whose localspace XY plane is used to perform IK calculations. Use this to define the Transform used.
This is the class which stores the transforms involved in an IK chain. When a chain is set up with a target and a transform count, initializing the Solver will populate the chain with the right transforms if valid.
Target - The transform to perform IK on to reach a desired position.
Effector - The transform which is used as the desired position for the target.
TransformCount - The number of transforms involved in the IK solution starting from the target. This is generally equivalent to ChainLength in solvers.
Transforms - All transforms involved in the chain. In general, the last transform in this is the target transform and the first transform is considered the root transform for the chain.
Lengths - The lengths between each transform in the chain.
This attribute allows you to tag your Solver2D with a different name under the IKManager2D. Use this if you do not want to use the name of the class of the Solver2D.
Example for LimbSolver2D:[Solver2DMenuAttribute("Limb")]