Applying Non-Uniform Scaling to Game Objects
In this lesson, you will learn how to stretch or squash your GameObjects by independently modifying the X, Y, and Z values of their local scale.
Understanding Non-Uniform Scaling
When you adjust the transform.localScale property of an object, you are defining how large it is relative to its parent. While uniform scaling keeps an object proportional (e.g., setting everything to 2), non-uniform scaling allows you to treat each axis as a unique dimension.
Think of it like working with a piece of clay: you can pull it along the X-axis to make it a long plank, stretch it on the Y-axis to make it a tall pillar, or flatten it on the Z-axis to make it a thin panel. Because the scale is a Vector3 struct, you have direct access to these three independent channels.
Implementation via Scripting
To change the scale through a script, you assign a new Vector3 to the transform.localScale property. Here is how you might stretch an object to look like a wide platform:
void Start()
{
// Sets the width to 5, height to 1, and depth to 2
// This creates a flat, wide shape regardless of the original size
transform.localScale = new Vector3(5.0f, 1.0f, 2.0f);
}
Why it Matters
Using non-uniform scaling is an incredibly efficient way to reuse basic shapes (like a single Cube primitive) to build complex level geometry. By creating a script that modifies these values, you can procedurally adjust the layout of your game world.
Pro Tip: Remember that when you scale an object non-uniformly, any child objects attached to it will also inherit that "stretched" coordinate space. If your child objects look distorted, you may need to adjust their local scales to compensate for the parent’s transformation.
Key Takeaway: By assigning a new Vector3 to transform.localScale, you can manipulate the X, Y, and Z axes independently to morph the shape of any GameObject in your scene.