Documentation

Unity Engine

Download Unity

Unity Engine

SolvePositionConstraint

Position constraint solver moves a constrained bone to follow its source objects.
Read time 3 minutesLast updated 4 hours ago

Definition

SolvePositionConstraint(AnimationStream, PositionConstraintData, float)

Position constraint solver moves a constrained bone to follow its source objects.
public static void SolvePositionConstraint(ref AnimationStream stream, in Core.PositionConstraintData data, float weight)

Parameters

The animation stream to apply the solver to.


weight

The overall weight of the solver, clamped to the range from 0 to 1. A weight of 0 leaves the constrained bone unchanged, and a weight of 1 applies the full constraint. Intermediate values blend linearly.

SolvePositionConstraint(AnimationStream, PositionConstraintData, float3, float3, float)

Applies a position constraint to a bone from a single source.
public static void SolvePositionConstraint(ref AnimationStream stream, in Core.PositionConstraintData data, float3 sourcePosition, float3 sourceOffset, float weight)

Parameters

The animation stream to apply the solver to.


sourcePosition

The source position, in character space. To follow a world-space target, convert it first with
math.transform(math.inverse(characterToWorld), worldPosition)
.

sourceOffset

The additional translation offset to apply on the source position.

weight

The overall weight of the solver, clamped to the range from 0 to 1. A weight of 0 leaves the constrained bone unchanged, and a weight of 1 applies the full constraint. Intermediate values blend linearly.

Remarks

Use this overload when the constraint has one source. It gives the same result as Core.SolvePositionConstraint with one source of weight 1, and you don't need to allocate a NativeArray<T> for the sources. The method ignores the source arrays in
data
.
The following example uses the
Unity.Animations
and
Unity.Mathematics
namespaces. It converts world-space targets to character space and passes them to the single-source solvers.

Examples

// This example makes a bone aim at a target that lives in world space, for example the position of another entity.// The constraint solvers work in character space, so the example converts the target first. characterToWorld// is the character's LocalToWorld transform. Its inverse takes a world-space point to character space.public static void AimAtWorldTarget( ref AnimationStream stream, int boneIndex, float3 aimAxis, RigidTransform characterToWorld, float3 worldTarget, float weight){ float3 characterTarget = math.transform(math.inverse(characterToWorld), worldTarget); // Start from the default settings. The solvers that take a single source ignore the source arrays, // so there is nothing to allocate. Core.AimConstraintData data = Core.AimConstraintData.Default(); data.Index = boneIndex; data.AimAxis = aimAxis; Core.SolveAimConstraint(ref stream, data, characterTarget, quaternion.identity, weight);}// A parent constraint follows a world-space transform the same way: multiply the target by the inverse of// characterToWorld to get a character-space transform.public static void FollowWorldTransform( ref AnimationStream stream, int boneIndex, RigidTransform characterToWorld, RigidTransform worldTarget, float weight){ RigidTransform characterTarget = math.mul(math.inverse(characterToWorld), worldTarget); Core.ParentConstraintData data = Core.ParentConstraintData.Default(); data.Index = boneIndex; Core.SolveParentConstraint(ref stream, data, characterTarget, RigidTransform.identity, weight);}// The position and rotation constraints convert a world-space target the same way.public static void MoveToWorldPosition( ref AnimationStream stream, int boneIndex, RigidTransform characterToWorld, float3 worldPosition, float weight){ float3 characterPosition = math.transform(math.inverse(characterToWorld), worldPosition); Core.PositionConstraintData data = Core.PositionConstraintData.Default(); data.Index = boneIndex; Core.SolvePositionConstraint(ref stream, data, characterPosition, float3.zero, weight);}public static void MatchWorldRotation( ref AnimationStream stream, int boneIndex, RigidTransform characterToWorld, quaternion worldRotation, float weight){ quaternion characterRotation = math.mul(math.inverse(characterToWorld.rot), worldRotation); Core.RotationConstraintData data = Core.RotationConstraintData.Default(); data.Index = boneIndex; Core.SolveRotationConstraint(ref stream, data, characterRotation, quaternion.identity, weight);}