Documentation

Unity Engine

Download Unity

Unity Engine

SolveTwoBoneIK(ref AnimationStream, in TwoBoneIKData, float)

Simple 2-bone IK solver based on cosine rule. It uses an additional hint position (specified in LocalToCharacter space) which allows you to specify the direction the limb should be oriented when it bends.
Read time 5 minutesLast updated 4 hours ago

Definition

SolveTwoBoneIK(AnimationStream, TwoBoneIKData, float)

public static void SolveTwoBoneIK(ref AnimationStream stream, in Core.TwoBoneIKData data, float weight)

Parameters

The animation stream to apply the solver to.

The solver settings. See Core.TwoBoneIKData.

weight

The overall weight of the solver. If set to 0, the solver has no influence on the limb elements. When set to 1, it applies full influence with the current settings.

Remarks

This solver does not work on non-uniform scaled hierarchies.
The target and the hint are in character space. Core.TwoBoneIKData.Default sets Core.TwoBoneIKData.TargetRotationWeight to 1, so the tip takes the rotation of the target. To keep the rotation of the tip, set the rotation of the target to it, or set the weight to 0.
The following example is a
MonoBehaviour
that plays a clip and makes an arm reach for a target, which it converts from world space to character space.
The last code block is the Burst-compiled function that writes the pose to the transforms. The function isn't part of the API, so copy it into your project.
``<![CDATA[using Unity.Burst; using Unity.Collections; using Unity.Mathematics; using UnityEngine;
namespace Unity.Animations.DocumentationExamples { // Writes the pose in a stream to the transform handles that // RigDefinitionUtility.BindTransforms outputs. Entry index of the // handles is the transform of bone index. [BurstCompile] public static class WriteTransformsUtility { // A Burst-compiled static method runs on the main thread, so it // writes the transforms without the cost of scheduling a job. [BurstCompile] public static void WriteTransforms( in AnimationStream stream, in NativeArray<TransformHandle> transforms) { for (int i = 0; i < transforms.Length; ++i) { // The entry of a bone that has no transform is None, and a // destroyed transform is no longer valid. TransformHandle transform = transforms[i]; if (!transform.IsValid()) { continue; }
stream.GetLocalToParentTRS( i, out float3 translation, out quaternion rotation, out float3 scale); transform.SetLocalPositionAndRotation(translation, rotation); transform.localScale = scale; } } } } ]]>

Examples

using Unity.Collections;using Unity.IntegerTime;using Unity.Mathematics;using UnityEngine;namespace Unity.Animations.DocumentationExamples{ // This example plays a clip and makes an arm reach for a target with two-bone IK, for example a door handle that // the character grabs. Assign a clip that animates the transforms under the GameObject of this component, with // paths relative to this GameObject, including the bones of the arm. Then assign the skeleton root, the top bone // of the skeleton, a direct child of this GameObject. Assign the bones of the arm and the target. Optionally, // assign a hint that the elbow bends towards. public class ArmReachExample : MonoBehaviour { public AnimationClip clip; public Transform skeletonRootTransform; public Transform upperArm; public Transform forearm; public Transform hand; public Transform target; public Transform elbowHint; [Range(0f, 1f)] public float ikWeight = 1f; // When true, the hand also turns to the rotation of the target, for example to grip a handle. When false, the // hand keeps the rotation of the clip. public bool matchTargetRotation = true; BlobObjectReference<RigDefinition> m_RigDefinition; NativeArray<TransformHandle> m_BoneTransforms; ClipSampler m_Sampler; int m_UpperArmIndex; int m_ForearmIndex; int m_HandIndex; float m_Time; void OnEnable() { if (clip == null || skeletonRootTransform == null) return; // The reference keeps the rig definition alive until you dispose it. using RigBuilder rigBuilder = RigBuilder.Create( transform, skeletonRootTransform, new[] { clip }, Allocator.Temp); m_RigDefinition = rigBuilder.ToRigDefinition(); RigDefinitionUtility.BindTransforms( ref m_RigDefinition.Value, transform, Allocator.Persistent, out m_BoneTransforms); m_Sampler = clip.ToClipSampler(ref m_RigDefinition.Value, Allocator.Persistent); // Find the bones of the arm once. The bound transforms are indexed by bone index, and a transform that // isn't a bone of the rig has the index -1. m_UpperArmIndex = FindBoneIndex(upperArm); m_ForearmIndex = FindBoneIndex(forearm); m_HandIndex = FindBoneIndex(hand); m_Time = 0f; } void OnDisable() { // Dispose the sampler before the rig definition that it uses. if (m_Sampler.IsCreated) m_Sampler.Dispose(); if (m_BoneTransforms.IsCreated) m_BoneTransforms.Dispose(); m_RigDefinition.Dispose(); } void Update() { m_Time += Time.deltaTime; ApplyPose(m_Time); } // Samples the clip at a time, moves the hand to the target, and writes the pose to the transforms. public void ApplyPose(float time) { if (!m_Sampler.IsCreated) return; AnimationStream stream = AnimationStream.Create(ref m_RigDefinition.Value, Allocator.Temp); m_Sampler.Sample(ref stream, new DiscreteTime(time)); if (target != null && m_UpperArmIndex >= 0 && m_ForearmIndex >= 0 && m_HandIndex >= 0) { // The upper arm, the forearm, and the hand form the limb. The target and the hint are in character // space, so convert them from world space. Core.TwoBoneIKData ik = Core.TwoBoneIKData.Default(); ik.RootIndex = m_UpperArmIndex; ik.MidIndex = m_ForearmIndex; ik.TipIndex = m_HandIndex; ik.Target = new RigidTransform( Quaternion.Inverse(transform.rotation) * target.rotation, transform.InverseTransformPoint(target.position)); // The default settings turn the hand to the rotation of the target. A rotation weight of 0 keeps the // rotation of the hand from the clip, relative to the character. ik.TargetRotationWeight = matchTargetRotation ? 1f : 0f; // Without a hint, the solver keeps the elbow in the plane in which the arm bends in the clip. A hint // pulls the elbow towards a point, for example behind and below the shoulder. if (elbowHint != null) { ik.Hint = transform.InverseTransformPoint(elbowHint.position); ik.HintWeight = 1f; } // The weight blends between the pose of the clip, at 0, and the solved pose, at 1. Raise it over a few // frames when the character starts to reach, so the arm doesn't jump. Core.SolveTwoBoneIK(ref stream, ik, ikWeight); } // Write the pose to the transforms with a Burst-compiled function. The function isn't part of the API, // so copy it into your project. WriteTransformsUtility.WriteTransforms(stream, m_BoneTransforms); // Dispose the stream explicitly, because a using declaration can't be passed by reference. stream.Dispose(); } // Returns the bone index of a transform, or -1 if no bone has the transform. int FindBoneIndex(Transform bone) { if (bone == null) return -1; for (int i = 0; i < m_BoneTransforms.Length; ++i) { if (m_BoneTransforms[i] == bone.transformHandle) return i; } return -1; } }}