---
title: SolveAimConstraint
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/solveaimconstraint
latest_url: https://docs.unity.com/en-us/engine/7000.0/script-reference/unity/animations/core/solveaimconstraint.md
last_updated: 2026-11-30
generated_at: 2026-11-30T21:10:49.082Z
locale: en-us
---

# SolveAimConstraint

> Aim constraint solver rotates a constrained bone to face a target position specified by one or more source objects.

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

## SolveAimConstraint(AnimationStream, AimConstraintData, float)

Aim constraint solver rotates a constrained bone to face a target position specified by one or more source objects.

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

### Parameters

**** (\[AnimationStream]\(/engine/7000.0/script-reference/unity/animations/animationstream)): The animation stream to apply the solver to.**** (\[AimConstraintData]\(/engine/7000.0/script-reference/unity/animations/core/aimconstraintdata)): The solver settings. See [Core.AimConstraintData](/engine/7000.0/script-reference/unity/animations/core/aimconstraintdata.md).**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): 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 the rotation with a normalized linear interpolation.

### Remarks

The following example is a `MonoBehaviour` that makes the chest and the head of a character look at weighted points of interest.

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 Unity.Collections;
using Unity.IntegerTime;
using Unity.Mathematics;
using UnityEngine;

namespace Unity.Animations.DocumentationExamples
{
    // This example plays an idle clip and makes the character look at points of interest, such as the player and a
    // nearby door. The chest turns a little towards them, and the head aims at them. Assign a clip that animates the
    // transforms under the GameObject of this component, with paths relative to this GameObject, including the chest
    // and the head. Then assign the skeleton root, the top bone of the skeleton, a direct child of this GameObject.
    // Assign the chest, the head, the points of interest, and the weight of each point.
    //
    // A point with twice the weight of another pulls the gaze twice as much. When the weights add up to less than 1,
    // the gaze only turns part of the way. In the default pose of the rig, the z-axis of the chest and of the head
    // points forward and their y-axis points up.
    public class AimConstraintExample : MonoBehaviour
    {
        public AnimationClip clip;
        public Transform skeletonRootTransform;
        public Transform chest;
        public Transform head;
        public Transform[] pointsOfInterest;
        public float[] interestWeights;

        BlobObjectReference<RigDefinition> m_RigDefinition;
        NativeArray<TransformHandle> m_BoneTransforms;
        ClipSampler m_Sampler;
        int m_ChestIndex;
        int m_HeadIndex;
        NativeArray<float3> m_Positions;
        NativeArray<float> m_Weights;
        NativeArray<quaternion> m_NoOffsets;
        float m_Time;

        void OnEnable()
        {
            if (clip == null || skeletonRootTransform == null || pointsOfInterest == null || interestWeights == null
                || pointsOfInterest.Length != interestWeights.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 chest and the head.
            m_ChestIndex = FindBoneIndex(chest);
            m_HeadIndex = FindBoneIndex(head);
            if (m_ChestIndex < 0 || m_HeadIndex < 0)
                return;

            m_Positions = new NativeArray<float3>(pointsOfInterest.Length, Allocator.Persistent);
            m_Weights = new NativeArray<float>(interestWeights, Allocator.Persistent);

            // Each source has a rotation offset, applied after the rotation that aims at it. These sources have none.
            m_NoOffsets = new NativeArray<quaternion>(pointsOfInterest.Length, Allocator.Persistent);
            for (int i = 0; i < m_NoOffsets.Length; ++i)
                m_NoOffsets[i] = quaternion.identity;
        }

        void OnDisable()
        {
            if (m_Positions.IsCreated)
                m_Positions.Dispose();
            if (m_Weights.IsCreated)
                m_Weights.Dispose();
            if (m_NoOffsets.IsCreated)
                m_NoOffsets.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, aims the chest and the head at the points of interest, and writes the pose to
        // the transforms.
        public void ApplyPose(float time)
        {
            if (!m_Positions.IsCreated)
                return;

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

            // The points are in character space. Convert them from world space, so that the character looks at the
            // points wherever it stands and however it turns.
            for (int i = 0; i < m_Positions.Length; ++i)
                m_Positions[i] = transform.InverseTransformPoint(pointsOfInterest[i].position);

            // The chest only turns around its y-axis. The mask selects the axes of the bone, in the space of its
            // parent, around which the constraint can rotate it. A weight of 0.3 turns it a third of the way.
            Core.AimConstraintData chestAim = Core.AimConstraintData.Default();
            chestAim.Index = m_ChestIndex;
            chestAim.AimAxis = new float3(0f, 0f, 1f);
            chestAim.LocalAxesMask = new bool3(false, true, false);
            chestAim.SourcePositions = m_Positions;
            chestAim.SourceWeights = m_Weights;
            chestAim.SourceOffsets = m_NoOffsets;
            Core.SolveAimConstraint(ref stream, chestAim, 0.3f);

            // Solve the head after the chest, because the head aims from where the chest puts it. RootUpAxis keeps the
            // y-axis of the head as close as possible to the up axis of the character, so the head doesn't tilt to the
            // side when it looks up or down.
            Core.AimConstraintData headAim = Core.AimConstraintData.Default();
            headAim.Index = m_HeadIndex;
            headAim.AimAxis = new float3(0f, 0f, 1f);
            headAim.UpAxis = new float3(0f, 1f, 0f);
            headAim.RootUpAxis = new float3(0f, 1f, 0f);
            headAim.SourcePositions = m_Positions;
            headAim.SourceWeights = m_Weights;
            headAim.SourceOffsets = m_NoOffsets;
            Core.SolveAimConstraint(ref stream, headAim, 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;
        }
    }
}
```

## SolveAimConstraint(AnimationStream, AimConstraintData, float3, quaternion, float)

Applies an aim constraint to a bone from a single source.

```csharp
public static void SolveAimConstraint(ref AnimationStream stream, in Core.AimConstraintData data, float3 sourcePosition, quaternion sourceOffset, float weight)
```

### Parameters

**** (\[AnimationStream]\(/engine/7000.0/script-reference/unity/animations/animationstream)): The animation stream to apply the solver to.**** (\[AimConstraintData]\(/engine/7000.0/script-reference/unity/animations/core/aimconstraintdata)): The solver settings. The source arrays are ignored.**** (\[float3]\(/engine/7000.0/script-reference/unity/mathematics/float3)): The source position, in character space. To aim at a world-space target, convert it first with `math.transform(math.inverse(characterToWorld), worldPosition)`.**** (\[quaternion]\(/engine/7000.0/script-reference/unity/mathematics/quaternion)): The rotation offset for the source, in the parent space of the constrained bone. The solver applies it after the rotation that aims the bone at the source.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): 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 the rotation with a normalized linear interpolation.

### Remarks

Use this overload when the constraint has one source. It gives the same result as [Core.SolveAimConstraint](/engine/7000.0/script-reference/unity/animations/core/solveaimconstraint.md) with one source of weight 1, and you don't need to allocate a [NativeArray\<T>](/engine/7000.0/script-reference/unity/collections/nativearray1.md) 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

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