My game is seriously messed up right now. I have a weapon system where when the player picks up the weapon, it makes the weapon a child of the player. For some odd reason, when I child a weapon, it gets a really random scale that is different for each axis. For example, the scale of one object was 10 on all axes, but upon childing it, the scale becomes like 49.1576 for one axis, 53.472 for another, and so on. I know this is completely random because it is different each time I pick up the weapon.
When you reparent an object, its position/rotation/scale values that are visible in the Inspector are recomputed to reflect any transformations of any parent objects (before and after). Meaning: the world coordinates of your object will remain the same, but the local coordinates will change.
In addition, it is especially problematic to reparent between objects/hierarchies that have non-uniform scales somewhere. Because if any object below an object with non-uniform scale also has any rotation values whatsoever, the will become distorted, and not in the way you intended them to.
Also, the real local scale is difficult to compute if rotations are present, that’s why there isn’t even an API function for that in Unity (there is only transform.lossyScale
).
So if you attach a weapon to a non-uniformly scaled arm that’s also rotated (or the hand is rotated, etc.), I’d assume the correct undistoted scale of the weapon is not/cannot be computed, or it will change in strange ways once you rotate any related part of your character.
The best solution is - don’t ever use non-uniform scales in object hierarchies, unless you know exactly what you are doing. If your character model already contains non-uniform scales, you should fix the original model. If that’s not possible, you’re probably out of luck. You may get lucky if you restructure your model in Unity, so that e.g. instead of attaching it to a non-uniform hand, you replace it with an empty, uniformly scaled object, attach the hand to it, and create a sibling with uniform scale, attach the weapon to that instead, and now make sure that your kinematics now influence that new parent, instead of the hand. But as you can see, this is very messy, and for many models probably not even possible.
Probably the easiest solution/workaround is, to not attach the weapon to the character at all. Instead, add a script to your weapon that merely copies world position and rotation of your hand onto the weapon.