0
\$\begingroup\$

I think this code is only simple math, but I'm posting it, because almost everyone fakes the scaling with unparenting, then parenting again.

Vector3 originalParentScale = new Vector3;
Vector3 originalScale = new Vector3;
void Awake(){
    originalScale = transform.localScale;
    originalParentScale = transform.parent.localScale;
}
void Update(){
    transform.localScale = (originalParentScale / transform.parent.localScale) * originalScale;
}

I think it can be even improved, so I would be happy if someone can improve it. It's challenge too, so happy programming :-)

\$\endgroup\$
1
  • \$\begingroup\$ There is one division and one multiplication... how do you want to improve it? Maybe by using integrals? ;-P \$\endgroup\$
    – t3chb0t
    Dec 13, 2016 at 19:23

1 Answer 1

1
\$\begingroup\$

I'm not very familiar with Unity3D, but are you absolutely 100% certain that transform.parent.localScale will never be 0? Otherwise, you should check for that before dividing.

\$\endgroup\$
1
  • \$\begingroup\$ Nothing in Unity3D prevents you from setting the localScale to (0, 0, 0) \$\endgroup\$
    – Stud
    Dec 22, 2016 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.