Search Unity

How to change rotation of a child gameobject in global without affecting its scale?

Discussion in 'Getting Started' started by aksh2143, Oct 12, 2015.

  1. aksh2143

    aksh2143

    Joined:
    Jul 6, 2015
    Posts:
    10
    I have a cube named "carBody" of scale (0.83,0.14,1.51). It has an empty gameobject of scale(1,1,1,) named "tire". And a sphere named "front tire" as a child of "tire".

    Now the problem is, when i change the rotation of "front tire", it also changes it's scale automatically as it is a chlid gameobject,

    i'm using this code to rotate:
    tire1.transform.eulerAngles = new Vector3 (10, 10, 10);

    but it's also changing the scale.
    How can i stop it from changing it's scale?
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,345
    If you're scaling the top-level parent, all children will be scaled as well. To avoid scaling children, your scaled object (the car body) should be a sibling of these other objects, not a parent. So the hierarchy would go like:

    - Car (Empty)
    -- Body (Scaled)
    -- Tire (Empty)
    --- Front Tire (Turns and does not scale)

    I'm not even sure if you need the Tire object, but your code would dictate that.
     
    aksh2143 likes this.
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,665
    Something's funny there. First of all, the code you're claiming you're using (@aksh2143) wouldn't compile; Transform doesn't have a eulerAngles property. Second, changing the rotation of an object does not change its scale, regardless of how your hierarchy is set up.

    So, I think you should post a screenshot of your hierarchy, and then copy/paste the actual, complete code you're using. (If that seems like too much, then you should first reduce it to a trivial example of the problem — and chances are good that while you attempt to do that, you'll solve it for yourself!)
     
    aksh2143 likes this.
  4. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    801
    er, yes it does: http://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html

    The reason Transform has both 'rotation' and 'eulerAngles' is because 'rotation' is a quaternion. Behind the scenes Unity handles rotation as a quaternion even if you define it using Euler angles, but it is often easier to write/understand in Euler angles so they provide a separate property to handle automatic conversion between quaternions/Euler angles. For example, the Inspector actually displays the rotation as Euler angles even though the rotation behind the scenes is done using quaternions.

    But yeah, this part of the original question I'm also confused by:
    Changing the scale of the parent could unexpectedly change the scale of child objects, but I don't see any way that changing something's rotation could change an object's scale.
     
    aksh2143 likes this.
  5. aksh2143

    aksh2143

    Joined:
    Jul 6, 2015
    Posts:
    10
    yes, thanx that worked well :)
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,665
    Well, shep my mouth and call me silly; I never realized that. I've been using transform.rotation.eulerAngles or transform.localRotation.eulerAngles when I need something like that.

    Out of curiosity, which of those is transform.eulerAngles a shortcut for? The docs don't seem clear on that.
     
  7. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    801
    There are properties for both 'eulerAngles' and 'localEulerAngles'; I imagine that makes it clear.
     
    JoeStrout likes this.
unityunity