Documentation

Unity Engine

Download Unity

Unity Engine

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.
Read time 4 minutesLast updated 4 hours ago

Definition

SampleRootMotion(TimerState)

public MotionOutput SampleRootMotion(in TimerState timer)

Parameters

The clip timer to sample, from Clip.ToTimerState.

Returns

Type

Description

MotionOutputThe root motion from TimerState.PreviousTime to TimerState.Time, and the root transform at TimerState.Time. Both transforms are the identity if ClipSampler.HasRootMotion is
false
.

Remarks

MotionOutput.RootMotion is the root transform of the clip at TimerState.Time, relative to its transform at TimerState.PreviousTime. For a looping clip, it includes the whole cycles that the timer crossed, from TimerState.PreviousLoopCount to TimerState.LoopCount. 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 is the root transform of the clip at TimerState.Time.
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.
If ClipSampler.HasRootMotion 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 or RootMotionUtility.DeltaMixer. RootMotionUtility has a helper for each other stream operation.
If ClipSampler.HasRootMotion is
true
, the first call allocates extra memory for the sampler, with the allocator that you passed to AnimationClipExtensions.ToClipSampler. ClipSampler.Dispose 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 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

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