Hi all. this seems to be a complex problem.
I need to be able to read, and also set, the global scale of an object, which is a child many layers deep in a hierarchy. The layers above it may have any arbitrary scales, rotations and positions
Apparently this is done using transform matrices, but i’m not aware of how to use them.
It can also apparently be done using the Transform.Transform* functions. But there are three of those, and I’m not sure which to use
Accuracy is more critical than performance here. I need a solution that works, even if slowly.
The programmer way to do this:
public static void SetGlobalScale (this Transform transform, Vector3 globalScale)
transform.localScale = Vector3.one;
transform.localScale = new Vector3 (globalScale.x/transform.lossyScale.x, globalScale.y/transform.lossyScale.y, globalScale.z/transform.lossyScale.z);
A cute trick is just to unparent the object, set the scale (which is still the localScale variable, but which is really global scale,) then reparent it. Unity will recompute the correct localScale (with respect to parent rotations and scales) for you.
If the parent rotations are such that you have non-uniform scaling problems, then it can’t be done. Not by you or Unity. In that case, “fake” it having a parent by having code track the position/rotation of a pretend parent.