- Home /
How can i get the total scale of an object?
So i have a game object which is buried an arbitrary number of layers deep in the hierarchy. And every parent object above it might have a different localscale value.
Is there any better way than recursing up the hierarchy multiplying them all, to find this objects absolute/total scaling value ?
sounds good. But imperfect.
How can i do it perfectly using a transform matrix? with this particular bit of code im doing, accuracy is most important, and i have a long time to calculate it. like, it wouldn't matter if this operation took several seconds.
Answer by Jonesy19 · May 21, 2015 at 08:59 PM
I have run into this same issue, and the only way I have been able to do it is (as you said) to recursively go up the tree and multiply the scales...I've looked around a lot for an answer and, unfortunately, that's the only way I've been able to do it. :(
but isn't this exactly what transform matrices are for? have you tried working with that?
failing that, if you got it working do you maybe have a code snippet i could use? ^-^
Sure, I didn't have it offhand, but I quickly threw this together... This is recursive, which is unfortunate, but I just tested it out to three layers deep and it gets the scale exactly.
void deter$$anonymous$$eScale()
{
Vector3 tempScale = new Vector3(1, 1, 1);
Transform t = this.transform;
while (t != null)
{
tempScale = new Vector3(tempScale.x * t.localScale.x, tempScale.y * t.localScale.y, tempScale.z * t.localScale.z);
t = t.parent;
}
totalScale = tempScale;
}
Your answer
Follow this Question
Related Questions
How to smooth transform scaling with 2d sprite 1 Answer
TextMeshPro Height 0 Answers
Gradually scale platform? 4 Answers
Can the insides of a gameObject affect its transform? 0 Answers