How to activate parent constraint via API the same way as Activate button does?
Asked Answered
R

4

0

I am trying to call the same function as Activate button of the Parent Constraint component but it seems there’s no such method in the API:

130776-2019-01-09-0022.png

Guess the code is hidden in the component’s editor script. Was trying to achieve the same result using SetTranslationOffset and SetRotationOffset which looked pretty straight forward but ended up with objects jump a bit when the function is called:

var positionDelta = parentConstraint.transform.position - source.position;
var rotationDelta = Quaternion.Inverse(source.rotation) * parentConstraint.transform.rotation;
parentConstraint.SetTranslationOffset(0, positionDelta);
parentConstraint.SetRotationOffset(0, rotationDelta.eulerAngles);
Revitalize answered 20/3, 2024 at 10:36 Comment(0)
I
0

So I had the same problem, and it seems like the ‘jump’ is due to the constraintTransform’s world rotation (the bigger rotation the bigger gap).

This worked for me:

parentConstraint.SetTranslationOffset(0, Quaternion.Inverse(constraintTransform.rotation) * positionDelta);
Indisposed answered 20/3, 2024 at 10:36 Comment(2)

I don't know WHY it works, but it works! Thanks!

Calash

mad life saver omy goodness I spent the last couple days pulling my hair out for this

Xylography
S
0

After you have added all your sources:

//This does the equivalent of pressing the "Activate" button
List<ConstraintSource> sources = new List<ConstraintSource>(constraint.sourceCount);
constraint.GetSources(sources);

for( int i=0 ; i<sources.Count ; i++ )
{
    Transform sourceTransform = sources.sourceTransform;
    Vector3 positionOffset = sourceTransform.InverseTransformPoint( constrainedTransform.position);
    _Quaternion rotationOffset = Quaternion.Inverse(sourceTransform.rotation) * transform.rotation;_
    constraint.SetTranslationOffset(i, positionOffset);
    constraint.SetRotationOffset(i, rotationOffset.eulerAngles);
}

//And remember to turn it on!
constraint.weight = 1f;//Master weight

constraint.constraintActive = true;
Specular answered 20/3, 2024 at 10:38 Comment(1)

Actually Vlogix's answer solves the jump issue. Unity Team, how about adding examples to Parent Constraint API after 3 years of feature existence?:)

Revitalize
J
0

Something that’s not addressed by the other comments so far: If there’s scaling involved, you have to explicitly ignore it when transforming for your TranslationOffset. That is:

Matrix4x4 inverse = Matrix4x4.TRS(constraintTransform.position, constraintTransform.rotation, new Vector3(1,1,1)).inverse;
parentConstraint.SetTranslationOffset(0, inverse.MultiplyPoint3x4(parentConstraint.transform.position));
parentConstraint.SetRotationOffset(0, (Quaternion.Inverse(constraintTransform.rotation) * parentConstraint.transform.rotation).eulerAngles);
Jackdaw answered 20/3, 2024 at 10:39 Comment(1)

So googling about this issue led me here, where I realized I myself posted an answer 4 years ago. But your answer helped me more than my mine did. Thank you!

Indisposed
A
0

Just want to add a note here in case it’s useful to anyone. I was struggling to set up a PositionConstraint in code. I realised that the translation offset needed to be the difference in positions in the space of the constrained objects parent. My code was like this (assumes parent is not null):

PositionConstraint constraint = ConstrainedGameObject.AddComponent<PositionConstraint>();
ConstraintSource constraintSource = new() { sourceTransform = SourceGameObject.transform, weight = 1.0f };
        
// Translation offset is in the parent space of the constrained object.
// So move the constraint source object into the parent space of the constrained object
// then set the translation offset to the difference between the two
constraint.translationOffset = ConstrainedGameObject.transform.localPosition - ConstrainedGameObject.transform.parent.InverseTransformPoint(SourceGameObject.transform.position);
constraint.constraintActive = true;
constraint.AddSource(constraintSource);
Agnomen answered 20/3, 2024 at 10:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.