---
title: SampleRootMotion(in TimerState)
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/clipsampler/samplerootmotion
latest_url: https://docs.unity.com/en-us/engine/7000.0/script-reference/unity/animations/clipsampler/samplerootmotion.md
last_updated: 2026-11-30
generated_at: 2026-11-30T21:17:53.267Z
locale: en-us
---

# SampleRootMotion(in TimerState)

> Samples how much the root of the clip moved from the previous time to the time of a clip timer, and the root transform at that time.

## 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)

## SampleRootMotion(TimerState)

```csharp
public MotionOutput SampleRootMotion(in TimerState timer)
```

### Parameters

**** (\[TimerState]\(/engine/7000.0/script-reference/unity/animations/timerstate)): The clip timer to sample, from [Clip.ToTimerState](/engine/7000.0/script-reference/unity/animations/clip/totimerstate.md).

### Returns

| Type                                                                             | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| -------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [MotionOutput](/engine/7000.0/script-reference/unity/animations/motionoutput.md) | The root motion from [TimerState.PreviousTime](/engine/7000.0/script-reference/unity/animations/timerstate/previoustime.md) to [TimerState.Time](/engine/7000.0/script-reference/unity/animations/timerstate/time.md), and the root transform at [TimerState.Time](/engine/7000.0/script-reference/unity/animations/timerstate/time.md). Both transforms are the identity if [ClipSampler.HasRootMotion](/engine/7000.0/script-reference/unity/animations/clipsampler/hasrootmotion.md) is `false`. |

### Remarks

[MotionOutput.RootMotion](/engine/7000.0/script-reference/unity/animations/motionoutput/rootmotion.md) is the root transform of the clip at [TimerState.Time](/engine/7000.0/script-reference/unity/animations/timerstate/time.md), relative to its transform at [TimerState.PreviousTime](/engine/7000.0/script-reference/unity/animations/timerstate/previoustime.md). For a looping clip, it includes the whole cycles that the timer crossed, from [TimerState.PreviousLoopCount](/engine/7000.0/script-reference/unity/animations/timerstate/previousloopcount.md) to [TimerState.LoopCount](/engine/7000.0/script-reference/unity/animations/timerstate/loopcount.md). Call this method once for each update of the timer, and accumulate the root motion after the transform of the character to move it, for example `character = math.mul(character, output.RootMotion)`.

[MotionOutput.RootTransform](/engine/7000.0/script-reference/unity/animations/motionoutput/roottransform.md) is the root transform of the clip at [TimerState.Time](/engine/7000.0/script-reference/unity/animations/timerstate/time.md). `Sample` writes the curves of the root motion bone into the pose, so the pose moves with the clip. To keep the root motion out of the pose while you move the character with this method, sample the pose at the same time with [ClipSampler.SampleExtractingRootMotion](/engine/7000.0/script-reference/unity/animations/clipsampler/sampleextractingrootmotion.md).

If [ClipSampler.HasRootMotion](/engine/7000.0/script-reference/unity/animations/clipsampler/hasrootmotion.md) is `false`, the clip has no root motion and both transforms are the identity.

The stream operations don't carry root motion. To blend the root motion of several clips, combine the results with the same weights as the poses, with [RootMotionUtility.Blend](/engine/7000.0/script-reference/unity/animations/rootmotionutility/blend.md) or [RootMotionUtility.DeltaMixer](/engine/7000.0/script-reference/unity/animations/rootmotionutility/deltamixer.md). [RootMotionUtility](/engine/7000.0/script-reference/unity/animations/rootmotionutility.md) has a helper for each other stream operation.

If [ClipSampler.HasRootMotion](/engine/7000.0/script-reference/unity/animations/clipsampler/hasrootmotion.md) is `true`, the first call allocates extra memory for the sampler, with the allocator that you passed to [AnimationClipExtensions.ToClipSampler](/engine/7000.0/script-reference/unity/animations/animationclipextensions/toclipsampler.md). [ClipSampler.Dispose](/engine/7000.0/script-reference/unity/animations/clipsampler/dispose.md) frees it.

The method chooses the curves of the root motion as an Animator does:

* If the clip has `MotionT` and `MotionQ` curves, the root motion comes from them.

* Otherwise, if the clip has `RootT` and `RootQ` curves, the root motion comes from them. The method then applies the root motion settings of the clip: **Bake Into Pose**, **Based Upon**, **Offset**, and the rotation of the root motion around the vertical axis only.

* Otherwise, the root motion comes from the transform of the root motion bone of the rig in character space. By default, the root motion bone is the root bone of the rig. To choose another bone, set [RigBuilder.RootMotionBoneId](/engine/7000.0/script-reference/unity/animations/rigbuilder/rootmotionboneid.md) when you build the rig.

For the root motion bone, the rotation of the root transform is relative to the rotation of the bone in the default pose of the rig. The `MotionT` and `MotionQ` curves give the root transform as they are, as in an Animator.

The following example uses the `Unity.Animations`, `Unity.Collections`, `Unity.Entities`, `Unity.IntegerTime`, `Unity.Mathematics`, and `UnityEngine` namespaces. It plays a clip and moves the GameObject of the component with the root motion of the clip.

### Examples

```csharp
// This example plays a clip and moves the GameObject of this component with the root motion of the clip.
public class RootMotionExample : MonoBehaviour
{
    public AnimationClip clip;

    BlobObjectReference<RigDefinition> m_RigDefinition;
    ClipSampler m_Sampler;
    TimerState m_Timer;
    NativeArray<AnimatedData> m_Pose;

    void OnEnable()
    {
        if (clip == null)
            return;

        // Create the rig, the sampler, the timer, and the pose buffer once, and keep them between frames.
        // Without root motion curves in the clip, the root motion comes from the first transform of the rig, which is
        // the transform of this component. To use another transform, pass a root motion transform to
        // RigBuilder.Create. The reference keeps the rig definition alive until you dispose it.
        using RigBuilder rigBuilder = RigBuilder.Create(transform, new[] { clip }, Allocator.Temp);
        m_RigDefinition = rigBuilder.ToRigDefinition();
        m_Sampler = clip.ToClipSampler(ref m_RigDefinition.Value, Allocator.Persistent);
        if (!m_Sampler.IsCreated)
        {
            m_RigDefinition.Dispose();
            return;
        }

        m_Timer = TimerState.Create(
            new DiscreteTimeInterval(DiscreteTime.Zero, new DiscreteTime(clip.length)),
            clip.isLooping ? WrapMode.Loop : WrapMode.ClampForever);

        // A stream can't be a field, so keep its buffer instead, and start from the default values of the rig.
        m_Pose = new NativeArray<AnimatedData>(m_RigDefinition.Value.Bindings.StreamSize, Allocator.Persistent);
        AnimationStream stream = AnimationStream.Create(ref m_RigDefinition.Value, m_Pose);
        stream.ResetToDefaultValues();
    }

    void OnDisable()
    {
        if (!m_RigDefinition.IsValid)
            return;

        // Dispose the sampler and the pose before the rig definition that they use.
        m_Pose.Dispose();
        m_Sampler.Dispose();
        m_RigDefinition.Dispose();
    }

    void Update()
    {
        Move(Time.deltaTime);
    }

    // Samples the clip for one frame, and moves the GameObject with the root motion of the clip.
    public void Move(float deltaTime)
    {
        // The rig isn't valid when the component has no clip, or the clip can't be sampled.
        if (!m_RigDefinition.IsValid)
            return;

        // The stream is a view of the pose buffer, so the pose stays between frames.
        AnimationStream stream = AnimationStream.Create(ref m_RigDefinition.Value, m_Pose);
        m_Timer.Update(new DiscreteTime(deltaTime));
        // Keep the root motion out of the pose. Otherwise the pose and the root motion both move the character.
        m_Sampler.SampleExtractingRootMotion(ref stream, m_Timer);

        // The root motion is the move of the root since the previous frame, relative to the character.
        MotionOutput motion = m_Sampler.SampleRootMotion(m_Timer);
        RigidTransform delta = motion.RootMotion;

        transform.SetPositionAndRotation(
            transform.position + transform.rotation * (Vector3)delta.pos,
            transform.rotation * delta.rot);
    }
}
```
