Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by tetto_green · Jan 08, 2019 at 09:32 PM · apiconstraints

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

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: alt text


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


2019-01-09-0022.png (10.6 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image troien · Jan 09, 2019 at 12:22 PM 0
Share

If you look at the decompiled editor script, you can see that these buttons call

  • ActivateAndPreserveOffset (for the activate button)

  • ActivateWithZeroOffset (for the zero button)

Both of these methods belong to the internal interface IConstraintInternal which ParentConstraint implements.

But sinse IConstraintInternal is... internal. It is not accessible to us (Without using reflection atleast).


I don't post this as an answer, because I don't know what these methods do exactly, and someone else might still know a way to do the same thing by using the methods/properties that are available to you.

avatar image tetto_green troien · Jan 17, 2019 at 04:33 PM 0
Share

That makes sense. Thank you!

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Volgix · Feb 25, 2019 at 07:02 PM

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);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image spilat12 · Feb 16, 2021 at 02:02 PM 0
Share

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

avatar image
2

Answer by AubreyH · May 24, 2019 at 01:53 PM

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[i].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;



Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tetto_green · Jun 15, 2020 at 05:30 PM 1
Share

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

avatar image
0

Answer by Defrag · Apr 10 at 01:54 AM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

102 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do you constrain googleMaps to a window? 0 Answers

Is the scripting API source code available? 2 Answers

Question regarding how Vector3 works 2 Answers

Is Input.GetAxis meant to return to 0 on key up? 1 Answer

How to implement leaderboard with social api 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges