- Home /
Child lossyScale changes after setting localRotation
Hey there Unity community! I've been working quite hard on a project lately with some colleagues, and I've just ran into something that I think might be a Unity bug itself. Anyways, I would love you to have a look at it and tell me if I've stuck my foot in at any point. This is the function causing the error:
public void _Place (Transform a_parent, Vector3 a_coordinates, bool a_beKinematic = false)
{
transform.parent = a_parent;
transform.localPosition = a_coordinates;
rigidbody.isKinematic = a_beKinematic;
//No parent, so the pickable is dropped
if (a_parent == null) {
transform.localEulerAngles = DropRotation;
} else {//Parented, so the pickable is equipped
transform.localEulerAngles = EquipRotation;
}
}
The point of the function is to parent / unparent the gameObject with this script attached to the one passed as a parameter. Because of some weird reason, when transform.localEulerAngles = EquipRotation happens (IE, the object has just been parented), the child (the gameObject running the script) changes its scale both numerically (which would make sense, given the fact that it has just been parented by an object with a scale different from (1,1,1)), AND graphically. This is, not only does it apply the localScale it should apply when being parented, but it also changes its global scale when set its localEulerAngles property.
This happens for setting both localEulerAngles and localRotation. If I comment out that line, everything works fine. I'm going nuts. Can somebody help me out?
Thanks beforehand
Seems like I've ran into similar issue today and, as I'm very new to Unity and game development in general, wasted a whole day trying to understand why a projectile detached from a rotating parent gun changes its scale radically. Everything up to root from that projectile has scale (1, 1, 1).
I've noticed that the larger the degree the gun rotated (from Vector3.right or -Vector3.right, I'm builiding a 2D game) the lesser the scale becomes. Also I've noticed that at the moment of changing the projectile parent localScale stays (1, 1, 1), while lossyScales changes to some weird number, which localScale catches up later.
Since my math and Unity knowledge suffers, the only way I could solve this problem is by explicitely setting 'projectile.localScale' to (1, 1, 1).
Script reference on lossyScale states "If your objects are not skewed the value will be completely correct and most likely the value will not be very different if it contains skew too." $$anonymous$$aybe it's one of those not that likely cases? But I tend to think it's some fault on my side. Did you manage to understand what was happening?
Answer by GameDevBryant · Oct 03, 2014 at 06:52 PM
I am running into this problem as well, and unfortunately I have not yet found a solution that I find ideal. I've been searching the forums trying to find a calculation or algorithm to reverse this.
Basically what is happening, is the transform of the gameObject becomes parented to something scaled non-uniformly and the rotation does not match which causes it to skew (scale is stored in 4 values, but the 4th is not visible/editable kind of like quaternions which accounts for the skew).
So the options I have seen to avoid this are to have the parent scaled uniformly (all the axes scaled the same), have the child and parent match their rotations, or have an intermediate empty game object with scale of (1,1,1) in between what is currently the parent and the child.
Good luck, and I hope that one of these will help you out!