Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


ConstraintSource

Represents a weighted position that can be used in a constraint.
Read time 1 minuteLast updated 5 days ago

Definition

public struct ConstraintSource

Remarks

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.

Examples

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

Property

Description

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