Search Unity

Resolved Transform children dependent from parent

Discussion in 'Getting Started' started by ARunitystudi, Aug 18, 2021.

  1. ARunitystudi

    ARunitystudi

    Joined:
    Apr 25, 2021
    Posts:
    21
    Hello, I have a parent game-object and I am rotating it this way:
    Code (CSharp):
    1. parent.transform.rotation = Quaternion.AngleAxis(-rotX, up) * parent.transform.rotation;
    2. parent.transform.rotation = Quaternion.AngleAxis(rotY, right) * parent.transform.rotation;
    Now, I want its child game-objects to stay in their place and not to be rotated with it. So, I am applying this rotation on them to transform them back to their original position:
    Code (CSharp):
    1. child.transform.rotation = Quaternion.AngleAxis(rotX, up) * parent.transform.rotation;
    2. child.transform.rotation = Quaternion.AngleAxis(-rotY, right) * parent.transform.rotation;
    When I rotate the children this way, only their orientation is changed, but they do not get back to their original place. How can I achieve that the children do not move when their parent is rotated? Thanks.
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    If you're trying to keep the transform values of something or some things independent from that of its/their parent, you might want to reconsider whether those objects should actually be children of that thing.

    Perhaps consider this setup:
    • BigObject
    • ChildObjectContainer
      • Child1
      • Child2
      • Child3
    The ChildObjectContainer object can have a script on it that just updates it's transform.position every frame to move it to BigObject's position. Obviously this only works if you generally don't want to be updating those child object's rotation with BigObject most of the time.

    If you do just want to keep the children object's rotation for this specific instance, look into localRotation. That updates the object's rotation relative to its parent, which should help you get where you want.
     
    JoeStrout likes this.
  3. ARunitystudi

    ARunitystudi

    Joined:
    Apr 25, 2021
    Posts:
    21
    Thanks, I also thought of changing the structure, but currently I had to leave it this way as it would require a high amount of changes and was the only way I could find to show my text (= the child) in the correct position which is related to the parent.

    Therefore, I tried the second suggestion to rotate the children back to their position after rotating the parent. But unfortunately I have the same problem as before. Only their orientation is changed, but they are not rotated back to their position, when I do it e.g. this way:

    Code (CSharp):
    1. parent.transform.rotation = Quaternion.AngleAxis(-rotX, up) * parent.transform.rotation;
    2. parent.transform.rotation = Quaternion.AngleAxis(rotY, right) * parent.transform.rotation;
    3.  
    4. child.transform.localRotation = Quaternion.AngleAxis(rotX, up) * parent.transform.rotation;
    5. child.transform.localRotation = Quaternion.AngleAxis(-rotY, right) * parent.transform.rotation;
    6.  
    I hope someone has an idea of how to fix it.
     
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    What if instead of setting the local rotation, you just cached the world rotation of the parent before rotating it, then set the child rotation to that cached value afterward?
     
  5. ARunitystudi

    ARunitystudi

    Joined:
    Apr 25, 2021
    Posts:
    21
    Thanks, I had also tested this before by saving the child rotation and applying it back afterwards, but it leads to the same problem. Only the orientation of the model is changed, but the position is not moved back. I tried it now again by saving the rotation of the parent, but it leads as expected to the same behaviour.

    Code (CSharp):
    1. Quaternion savedRotation = parent.transform.rotation;
    2.  
    3. //<rotate parent as posted above>
    4.  
    5. child.transform.rotation = savedRotation;
     
  6. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Have you tried caching/setting the rotation and position of the parent/child, then?
     
    ARunitystudi likes this.
  7. ARunitystudi

    ARunitystudi

    Joined:
    Apr 25, 2021
    Posts:
    21
    Thank you very much, this really worked. :)
     
  8. purpl3grape

    purpl3grape

    Joined:
    Oct 18, 2015
    Posts:
    8
    I haven't tested yet, but will updating child rotation in LateUpdate() work?