Documentation

Unity Engine

Download Unity

Unity Engine

BlendSpaceUtility

Provides methods that compute the weights of the animations in one-dimensional and two-dimensional blend spaces.
Read time 2 minutesLast updated 3 hours ago

Definition

public static class BlendSpaceUtility

Remarks

A blend space mixes several animations according to an input value. The class has one method for each kind of blend space:
In a one-dimensional blend space, each animation is tied to a value on a line, and
ComputeBlendSpace1DWeights
converts the input value into one weight per animation. When the input matches a value exactly, that animation gets a weight of 1. When the input falls between two values, those two animations share the weight in proportion to the distance, and every other animation gets a weight of 0. When the input is outside the range of the values and wrapping is off, the first or last animation gets a weight of 1.
The following example uses the
Unity.Animations
and
Unity.Collections
namespaces. It computes the weights of walk, jog, and run streams for a speed, then blends the streams into one output stream.
Additional Resources: AnimationStreamBlendingExtensions

Examples

// This example blends walk, jog, and run streams according to a movement speed.// The walk, jog, and run animations are placed at speeds 1, 3, and 6 in the blend space.public static void BlendLocomotion( in AnimationStream walk, in AnimationStream jog, in AnimationStream run, float speed, ref AnimationStream output){ // The movement speed that each stream represents, in the order walk, jog, and run. using NativeArray<float> animationSpeeds = new NativeArray<float>(new float[] { 1f, 3f, 6f }, Allocator.Temp); NativeArray<float> weights = new NativeArray<float>(3, Allocator.Temp); // Compute one weight per stream. At a speed of 2, walk and jog each get a weight of 0.5. // Speeds outside the range from 1 to 6 give all the weight to the nearest stream, because wrapping is off. // The two 0f arguments are the wrap limits, which the call ignores when wrapping is off. BlendSpaceUtility.ComputeBlendSpace1DWeights( speed, animationSpeeds, false, 0f, 0f, weights); // Start from the walk stream, then blend in each other stream with a weight // relative to the total weight accumulated so far. output.CopyFrom(walk); float accumulatedWeight = weights[0]; if (weights[1] > 0f) { accumulatedWeight += weights[1]; output.Blend(jog, weights[1] / accumulatedWeight); } if (weights[2] > 0f) { accumulatedWeight += weights[2]; output.Blend(run, weights[2] / accumulatedWeight); } // Dispose the weights explicitly, because a using declaration can't be passed by reference. weights.Dispose();}

Static Methods

Method

Description

ComputeBlendSpace1DWeightsCompute 1D blend space weights given an input, wrap information and animation stream values.
ComputeBlendSpace2DFreeformWeightsComputes the weights of a 2D freeform blend space.
ComputeBlendSpace2DSimpleDirectionalWeightsComputes the weights of a 2D simple directional blend space.