Version: Unity 6.2 Alpha (6000.2)
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.
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961