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

Question Rotate child without deforming it (without Gimbal lock)

Discussion in 'Scripting' started by A_Box, Aug 14, 2023.

  1. A_Box

    A_Box

    Joined:
    Feb 3, 2020
    Posts:
    62
    Hello,
    I am trying to rotate a transform so that it is always facing up (gobal). This transform is a child of another gameobject that has NOT a (1,1,1) scale: this results in the child deforming when rotating.

    I know this is a normal phenomenon, but i couldnt find a way to counter its effects whitout also implementing a "Gimbal lock", which is not ideal since i'm tryng go make a flying object that should freely move in the tridimensional space.

    Is there some kind of solution to this other than changing the parenting hierarchy?

    Thanks for reading
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    the only way I was able to fix this, since children scale off their parent, is make a separate parent for the "hinge" of the object that needs rotating.

    So say you had a "body" parent with scale (1.5,2,1), and the arm was childed to that, if you rotate the arm it's trying to base it's scale off the body scale. So make a "spine"(empty gameObject) with scale (1,1,1), child the body to that. Then make a "shoulder"(empty gameObject) with scale (1,1,1) and make it a child of "spine" and child the arm to "shoulder".

    This way, each rotating position is properly scaled to (1,1,1)
     
    Bunny83 and A_Box like this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    It may actually suffice to „counterscale“ the child. If the parent is scaled 2,3,4 the child scale would have to be 1/2,1/3,1/4 to be back at a 1,1,1 world scale. Not sure if this affects rotation but I had to do that in the past for some reason I can‘t remember.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    By and large, it's good practice that any object that has children should just never be scaled. Put the mesh or whatever it is that needs scaling as a child, and leave the parent at 1,1,1.

    This would break the instant you tried to rotate the child.
     
    Bunny83 and karliss_coldwild like this.
  5. A_Box

    A_Box

    Joined:
    Feb 3, 2020
    Posts:
    62
    Thanks for the answers, i'll change the hierarchy then and make sure the parent is 1,1,1
     
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,494
    Yes, this even increases performance / reduces overhead as Unity can actually optimise hierarchies which are uniformly scaled or not scaled at all. Non uniform scaling can break many things such as batching. While it's convenient to scale objects as you see fit, it's better when things already have the proportions they should have.
     
    wideeyenow_unity likes this.