When doing scaling of rotated GameObjects in Unity it is very important to understand some things.

The way scaling works is by setting the localScale property of the Transform of the GameObject. The scaling is always relative to the parent. If there is no parent GameObject the scaling is of the object itself. An interesting fact is scaling by removing temporarily the parent. This allows you not to be depend to the scale factor of the parent.

 
    class ObjectScaler : MonoBehaviour
    {
        private Transform parent = null;

        public void ScaleWithoutParent(Vector3 scale)
        {
            // store parent
            parent = gameObject.transform.parent;

            // remove parent from gameobject
            gameObject.transform.parent = null;

            // scale
            gameObject.transform.localScale = scale;

            // set the parent back 
            gameObject.transform.parent = parent;

        }
    }

Another issue you get with scaling is when the GameObject is rotated. Scaling will be approximately based on the amount of rotation. Meaning that you can’t be sure that the scaling is exact every time. If you set the scale of a rotated GameObject from for example the Update() method, the size of the GameObject will differ everytime and causing it to bounce like a heart. If you look closely you will notice that it is always the same range of values.

To resolve this issue you will need to reset to rotation to something we call “no-rotation”. This is achieved by setting the rotation of the GameObject to Quaternion.Identity.

   class ObjectScaler : MonoBehaviour
    {
        private Quaternion prevRotation;

        public void ScaleMyObject(Vector3 scale)
        {
            // store rotation
            prevRotation = gameObject.transform.rotation;

            // reset to no-rotation
            gameObject.transform.rotation = Quaternion.identity;

            // scale
            gameObject.transform.localScale = scale;

            // set rotation
            gameObject.transform.rotation = prevRotation;
        }
    }

A simple code example above. Keep in mind that we do not check if the parent of their parent(s) are rotated. If you use one or more parents (E.g. pivoting a GameObject) you will need to reset their rotation too.

Be aware! if your GameObject has one or more rotated GameObjects in the parent chain you will need to reset their rotation as well. Otherwise you will still experience incorrect values.

 

Previous articleWordt 2018 het jaar van de HoloLens? Absoluut!
Next articleHoloLens and Unity tip #002 – Set your application icon and splash screen to beautify your app
A professional which inspires, motivates and educates businesses on how to leverage emerging technologies using Virtual, Augmented and Mixed Reality in their journey to digital innovation. He has a strong background on SharePoint, Office 365 and Microsoft Azure and is working with machine learning, artificial intelligence and cognitive services. Primarily focused on manufacturing, construction, industry, logistics and maritime/offshore, his goal is helping businesses to achieve more on creating, improving, smartening and shortening of business processes by using services, apps and devices. Furthermore, he engages in speaking, blogging and is organizer of events like the Mixed Reality User Group, Mixed Reality Talk @ VR, SP&C and the Global AI/MR Bootcamp. With more than 20 years of IT Business experience, acting in different roles like solution architect, he believes the future is just starting! Highly experienced in all realities, HoloLens and other devices, User experiences on devices and connecting the world to the cloud will help him to shape that future!

LEAVE A REPLY

Please enter your comment!
Please enter your name here