---
title: SolveTwistCorrection(ref AnimationStream, in TwistCorrectionData, float)
product: Unity Editor
version: '7000.0'
latest_version: '7000.0'
status: This page documents version 7000.0. The latest version is 7000.0.
canonical: https://docs.unity.com/en-us/engine/7000.0/script-reference/unity/animations/core/solvetwistcorrection
latest_url: https://docs.unity.com/en-us/engine/7000.0/script-reference/unity/animations/core/solvetwistcorrection.md
last_updated: 2026-11-30
generated_at: 2026-11-30T21:14:22.793Z
locale: en-us
---

# SolveTwistCorrection(ref AnimationStream, in TwistCorrectionData, float)

> Twist correction solver redistributes a percentage of a source's rotation to one or more twist nodes in order to correct the twist deformation of a body part that rotates on multiple axes.

## Definition

* **Type:** Method
* **Namespace:** [Unity.Animations](/engine/7000.0/script-reference/unity/animations.md)
* **Assembly:** [UnityEngine.AnimationCoreModule](/engine/7000.0/script-reference/modules/unityengine-animationcoremodule.md)

## SolveTwistCorrection(AnimationStream, TwistCorrectionData, float)

```csharp
public static void SolveTwistCorrection(ref AnimationStream stream, in Core.TwistCorrectionData data, float weight)
```

### Parameters

**** (\[AnimationStream]\(/engine/7000.0/script-reference/unity/animations/animationstream)): The animation stream to apply the solver to.**** (\[TwistCorrectionData]\(/engine/7000.0/script-reference/unity/animations/core/twistcorrectiondata)): The solver settings. See [Core.TwistCorrectionData](/engine/7000.0/script-reference/unity/animations/core/twistcorrectiondata.md).**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The overall weight of the solver. If set to 0, the solver has no influence on the twist nodes. When set to 1, it applies full influence with the current settings. Intermediate values are interpolated linearly.

### Remarks

The solver extracts the twist of [Core.TwistCorrectionData.SourceRotation](/engine/7000.0/script-reference/unity/animations/core/twistcorrectiondata/sourcerotation.md) relative to the default rotation of the source, around [Core.TwistCorrectionData.LocalTwistAxis](/engine/7000.0/script-reference/unity/animations/core/twistcorrectiondata/localtwistaxis.md). The axis must be the x-, y-, or z-axis. The solver sets the local rotation of each twist node to its share of that twist, and replaces the default rotation of the node, so use twist nodes whose default rotation is the identity rotation.

The following example is a `MonoBehaviour` that plays a clip and spreads the twist of the hand over two twist bones of the forearm.

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

```csharp
using System;
using Unity.Collections;
using Unity.IntegerTime;
using Unity.Mathematics;
using UnityEngine;

namespace Unity.Animations.DocumentationExamples
{
    // This example plays a clip and spreads the twist of the hand over the twist bones of the forearm, so that the skin
    // of the forearm turns gradually instead of all at the wrist. Assign a clip that animates the transforms under the
    // GameObject of this component, with paths relative to this GameObject. The clip must animate the twist bones too:
    // a clip that you import from a model usually has curves for every bone. Then assign the skeleton root, the top
    // bone of the skeleton, a direct child of this GameObject. Assign the hand, the twist bones, and the weight of each
    // twist bone.
    //
    // The twist bones are children of the forearm, like the hand, and have no children. In the default pose, their
    // rotation is the identity rotation and their x-axis points along the forearm, like the x-axis of the hand.
    public class TwistCorrectionExample : MonoBehaviour
    {
        public AnimationClip clip;
        public Transform skeletonRootTransform;
        public Transform hand;
        public Transform[] twistBones;

        // Each twist bone takes its weight times the twist of the hand. For example, with weights of 1/3 and 2/3, the
        // twist increases in steps along the forearm. A negative weight twists the bone in the other direction.
        public float[] twistWeights = { 1f / 3f, 2f / 3f };

        BlobObjectReference<RigDefinition> m_RigDefinition;
        NativeArray<TransformHandle> m_BoneTransforms;
        ClipSampler m_Sampler;
        int m_HandIndex;
        NativeArray<int> m_TwistIndices;
        NativeArray<float> m_TwistWeights;
        float m_Time;

        void OnEnable()
        {
            if (clip == null || skeletonRootTransform == null || twistBones == null || twistWeights == null
                || twistBones.Length != twistWeights.Length)
                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);
            m_Time = 0f;

            // Find the bones once. A transform that isn't a bone of the rig has the index -1, so check that the clip
            // animates the hand and the twist bones.
            m_HandIndex = FindBoneIndex(hand);
            var twistIndices = new int[twistBones.Length];
            for (int i = 0; i < twistIndices.Length; ++i)
                twistIndices[i] = FindBoneIndex(twistBones[i]);

            if (m_HandIndex < 0 || Array.IndexOf(twistIndices, -1) >= 0)
                return;

            m_TwistIndices = new NativeArray<int>(twistIndices, Allocator.Persistent);
            m_TwistWeights = new NativeArray<float>(twistWeights, Allocator.Persistent);
        }

        void OnDisable()
        {
            if (m_TwistIndices.IsCreated)
                m_TwistIndices.Dispose();
            if (m_TwistWeights.IsCreated)
                m_TwistWeights.Dispose();

            // 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, distributes the twist of the hand, and writes the pose to the transforms.
        public void ApplyPose(float time)
        {
            if (!m_TwistIndices.IsCreated)
                return;

            AnimationStream stream = AnimationStream.Create(ref m_RigDefinition.Value, Allocator.Temp);
            m_Sampler.Sample(ref stream, new DiscreteTime(time));

            // The solver extracts the twist of the hand relative to its default rotation. The twist axis must be the
            // x-, y-, or z-axis of the hand.
            AnimationStream defaultPose = stream.GetDefaultValues();

            Core.TwistCorrectionData twist = Core.TwistCorrectionData.Default();
            twist.SourceRotation = stream.GetLocalToParentRotation(m_HandIndex);
            twist.SourceInverseDefaultRotation = math.inverse(defaultPose.GetLocalToParentRotation(m_HandIndex));
            twist.LocalTwistAxis = new float3(1f, 0f, 0f);
            twist.TwistIndices = m_TwistIndices;
            twist.TwistWeights = m_TwistWeights;

            Core.SolveTwistCorrection(ref stream, twist, 1f);

            // 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;
        }
    }
}
```
