Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Discussion Parent Constraint on deactived GameObject

Discussion in 'Editor & General Support' started by Featrem, Jul 25, 2023.

  1. Featrem

    Featrem

    Joined:
    Mar 29, 2017
    Posts:
    6
    Hello,

    I have an object attached to another with Unity's ParentConstraint component. It may happen that this object is deactivated, so the ParentConstraint no longer works but I would like it to always update the position of the object even if it is deactivated.
    There is no built-in public method to force the update of the ParentConstraint.
    I tried to write by myself what could be the update method but I struggle.

    Can someone help me ?

    Thank you for your help
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    The Transform on the GameObject where it the ParentConstraint is located should suffice for moving it when it is disabled.

    What documentation have you been working from? The official Unity docs I'm looking at have a fair number of methods for changing stuff on a ParentConstraint.

    Otherwise, here is ...

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    Rearrange your hierarchy such that the ParentConstraint is on an object that is always active, and the other things are on perhaps a child object that becomes deactivated.
     
  4. Featrem

    Featrem

    Joined:
    Mar 29, 2017
    Posts:
    6
    Thanks for your feedback.
    English is not my native language, sorry if I misspoke.

    I would simply like the position/rotation of the gameobject which has a ParentConstraint to move normally, even if this GameObject is deactivated.

    Indeed, in the ParentConstraint documentation, there are plenty of public properties but there is no update method to force the update of the ParentConstraint.

    Basically, I would like to rewrite the Update method of the ParentConstraint, based on all the public properties that Unity gives us. So I could call this method, and the gameobject would take the correct position, as if the ParentConstraint was active.

    Here's what I've written so far, but it doesn't work for all cases. I think I have confusions between local, global, positions, offsets :

    Code (CSharp):
    1. public static void ForceUpdate(this ParentConstraint parentConstraint)
    2.     {
    3.         if (parentConstraint != null && parentConstraint.constraintActive)
    4.         {
    5.             // Calculate the final position and rotation based on the constraint sources and weights
    6.             Vector3 finalPosition = parentConstraint.translationAtRest;
    7.             Vector3 finalRotation = parentConstraint.rotationAtRest;
    8.  
    9.             if (parentConstraint.sourceCount > 0)
    10.             {
    11.                 // Loop through each source and apply the appropriate weight to the final position and rotation
    12.                 for (int i = 0; i < parentConstraint.sourceCount; i++)
    13.                 {
    14.                     ConstraintSource constraintSource = parentConstraint.GetSource(i);
    15.                     if (constraintSource.sourceTransform == null)
    16.                     {
    17.                         Debug.LogWarning("A constraint source is null.");
    18.                         continue;
    19.                     }
    20.  
    21.                     // Calculate the weighted position and rotation offset for this source
    22.                     Vector3 weightedPositionOffset = parentConstraint.translationOffsets[i] * constraintSource.weight;
    23.                     Vector3 weightedRotationOffset = parentConstraint.rotationOffsets[i] * constraintSource.weight;
    24.  
    25.                     // Add the weighted position and rotation offset to the final position and rotation
    26.                     finalPosition += constraintSource.sourceTransform.position + weightedPositionOffset;
    27.                     finalRotation += constraintSource.sourceTransform.eulerAngles + weightedRotationOffset;
    28.                 }
    29.             }
    30.  
    31.             // Apply the constraint weight to the final position and rotation
    32.             finalPosition = Vector3.Lerp(parentConstraint.translationAtRest, finalPosition, parentConstraint.weight);
    33.             finalRotation = Vector3.Lerp(parentConstraint.rotationAtRest, finalRotation, parentConstraint.weight);
    34.  
    35.             // Apply axis freezing
    36.             if ((parentConstraint.translationAxis & Axis.X) != Axis.X) finalPosition.x = parentConstraint.transform.position.x;
    37.             if ((parentConstraint.translationAxis & Axis.Y) != Axis.Y) finalPosition.y = parentConstraint.transform.position.y;
    38.             if ((parentConstraint.translationAxis & Axis.Z) != Axis.Z) finalPosition.z = parentConstraint.transform.position.z;
    39.             if ((parentConstraint.rotationAxis & Axis.X) != Axis.X) finalRotation.x = parentConstraint.transform.rotation.eulerAngles.x;
    40.             if ((parentConstraint.rotationAxis & Axis.Y) != Axis.Y) finalRotation.y = parentConstraint.transform.rotation.eulerAngles.y;
    41.             if ((parentConstraint.rotationAxis & Axis.Z) != Axis.Z) finalRotation.z = parentConstraint.transform.rotation.eulerAngles.z;
    42.  
    43.             // Apply the final position and rotation to the GameObject
    44.             parentConstraint.transform.position = finalPosition;
    45.             parentConstraint.transform.rotation = Quaternion.Euler(finalRotation);
    46.         }
    47.     }
    Thank you for your answer but rearranging the hierarchy is not an option in my project case.
     
  5. evyatron

    evyatron

    Joined:
    Jul 20, 2014
    Posts:
    132
    Here's a (hacky) idea - you could try calling the Update method yourself:
    Code (CSharp):
    1. m_ParentConstraint.SendMessage( "Update" );
    I don't know if this would work correctly (Unity's own methods might have something in there that blocks it from running), but might be worth a try.
     
  6. Featrem

    Featrem

    Joined:
    Mar 29, 2017
    Posts:
    6
    Clever, but unfortunately it doesn't work :(
     

    Attached Files:

  7. evyatron

    evyatron

    Joined:
    Jul 20, 2014
    Posts:
    132
    Ah of course you can't send messages to inactive game objects, duh!

    In a similar vein, you could maybe use Reflection to call the Update method yourself?
    You can see the file here:
    https://github.com/Unity-Technologi...n/ScriptBindings/ParentConstraint.bindings.cs

    Could maybe glean something from there?
    I'm not sure where the actual code is called, could be in IConstrainInternal which is not on GitHub, but maybe digging around using Reflection could yield something interesting!
     
  8. Featrem

    Featrem

    Joined:
    Mar 29, 2017
    Posts:
    6
    I'm not familiar with Reflection, good idea.

    I tried to get every method I could using Reflection. But none like what the Update could do, even in non-public.

    I will try to improve my "ForceUpdate" code above. I think its the only way to go. Do reverse code and rewrite ParentConstraint built-in Update method myself.
     
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    Good lord I can't believe you're entertaining using reflection instead of just rearranging things a little such that the constraint is never deactivated.
     
  10. Featrem

    Featrem

    Joined:
    Mar 29, 2017
    Posts:
    6
    This is a 6 years project with lot of developpers, I can't rearrange things without risking everything breaking.

    If it was so easy, I wouldn't ask for help on the forum.
     
  11. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    In large projects it is expected to constantly be redoing, rewriting, and rearranging things.
     
  12. Featrem

    Featrem

    Joined:
    Mar 29, 2017
    Posts:
    6
    ok thanks man
    But when I say I can't, it's that I can't.
    I know my job and my project. We already have and are doing refactoring.
    But in this specific case, the things that should be changed to do what I want with the ParentConstaint, are scripts and operations that touch the core of our software. It would be a huge refactoring job. And we don't have the time and especially not the money to do it. I'm a developer, it's not me who decides but my managers.
    If it was that easy, I wouldn't have opened this thread.