Search Unity

Changing parent rotation to match that of the child

Discussion in 'Getting Started' started by ShinigamiUzi, Nov 18, 2018.

  1. ShinigamiUzi

    ShinigamiUzi

    Joined:
    Sep 5, 2017
    Posts:
    54
    Hey guys, first off i'm really bad at explaining things so i'm using pictures

    I have child object, which I sometimes add some extra value on the local Y axis, this child holds the 3d model, for example, say I've added "38" on the child's local Y, result will be:

    1- The parent Forward direction:


    2- The Child Local direction (it's perfect with model since it has the model)





    On keydown, I stop adding value to the child yaw + I want the parent forward match the child's forward, however i'am really not sure how to do this in a way it won't affect other axis!

    I would really appreciate some tips !
     
  2. smallg2023

    smallg2023

    Joined:
    Sep 2, 2018
    Posts:
    147
    might not be the best method but it will work -> un-parent the child, rotate the (old) parent to match the (old) child and then re-parent the parent to the child.
    i.e.
    *assuming child id = childID and parent = parentID*
    childID.transform.parent = null;
    Quaternion crot = childID.transform.rotation;
    parentID.transform.rotation = new Quaternion(crot);
    childID.transform.parent = parentID.transform;

    though if the parent has other children then you would need to unparent / reparent all of them too same as the child... might be a better solution in that case but I'm not sure what that would be :)
     
    ShinigamiUzi likes this.
  3. ShinigamiUzi

    ShinigamiUzi

    Joined:
    Sep 5, 2017
    Posts:
    54
    how that work if both objects are moving at a high speed? i'll try It and see, thanks
     
  4. fct509

    fct509

    Joined:
    Aug 15, 2018
    Posts:
    108
    Another option would be to pull the rotation value from the child's Transformation, set that to the rotation value of the parent's Transformation, and then that to the rotation of the child's Transformation.

    The code will look a bit like this:
    Code (CSharp):
    1. void RotateParent(Transformation child)
    2. {
    3.     var childRot = child.rotation;
    4.     //var childPos = child.position;
    5.     child.parent.transformation.rotation = childRot;
    6.     child.rotation = childRot;
    7.     //child.position = childPos;
    8. }
    9.  
    What this code does is pull the rotation of the child object and converts it to world space, and then stores that in 'childRot'. Then it pulls the parent Transformation from the child, and sets its World-Space rotation to the value stored in 'childRot'; this will rotate all of the children the parent has, including the child GameObject. I should mention that when you set that value, the game engine will do the math to figure out a Local-Space rotation for the parent that matches up to the given World-Space rotation and then set that as the parent's local-rotation, which will then trickle down to the children as they update to match the change in their Parent-Space. Once all that has been done, you are then rotating the child back to it's original rotation in World-Space; which is done by calculating the Local-Space equivalent of the given World-Space value and setting that as the local rotation. I also added two lines of code that are commented out that will restore the child's position in World-Space. Unless the child is at the origin point within its Parent's Space, the child's position in World-Space will change as the parent rotates; those two lines will stop that from happening. Do remember, if the child's position changes due to parent rotation, then it may look like it's moving on top of it's parent as the parent rotates. From what I've read, this type of effect doesn't always work so well in game-engines.
     
    Last edited: Nov 28, 2018