Version: Unity 6.1 Alpha (6000.1)
LanguageEnglish
  • C#

ConstraintSource

struct in UnityEngine.Animations

/

Implemented in:UnityEngine.AnimationModule

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Represents a weighted position that can be used in a constraint.

Use this struct to provide a weighted position to a constraint that implements the IConstraint interface. You can use many constraint sources in a constraint. To adjust the effect these sources have on the constraint, set the weight parameter.

Additional resources: IConstraint PositionConstraint RotationConstraint ScaleConstraint AimConstraint ParentConstraint

using UnityEngine;
using UnityEngine.Animations;

// The example creates a new MonoBehaviour that is used alongside a ParentConstraint component.
// At runtime, the component adds two sources to the parent constraint and uses the property
// 'switchSource' to dynamically switch from one to the other.
[RequireComponent(typeof(ParentConstraint))]
public class ConstraintSourceSwitcher : MonoBehaviour
{
    private ParentConstraint m_ParentConstraint;

    public Transform source1;
    public Transform source2;

    public bool switchSource;

    void OnEnable()
    {
        m_ParentConstraint = GetComponent<ParentConstraint>();
        m_ParentConstraint.AddSource(new ConstraintSource { sourceTransform = source1});
        m_ParentConstraint.AddSource(new ConstraintSource { sourceTransform = source2});
    }

    public void Update()
    {
        m_ParentConstraint.SetSource(0, new ConstraintSource
        {
            sourceTransform = source1,
            weight = switchSource ? 0f : 1f
        });
        m_ParentConstraint.SetSource(1, new ConstraintSource
        {
            sourceTransform = source2,
            weight = switchSource ? 1f : 0f
        });
    }
}

Properties

sourceTransformThe transform component of the source object.
weightThe weight of the source in the evaluation of the constraint.