Skip to content
Home » Tutorials » Scale in one direction or between two points

Scale in one direction or between two points

Go directly to Git Gist.

Scale in one direction

When you scale an object in Unity you scale around the center point, if you instead would want to scale the object in one direction only, like making it bigger in the positive Y-axis, the following function will do that.

In this case I´m creating an extension method for the Transform component.

The method takes in two parameters: a Vector3 representing the direction in which to scale the object, and a float value representing the amount to scale the object. The direction parameter is a normalized vector that specifies the direction in which the object should be scaled.

The direction vector is transformed to take into account the object’s local rotation. This is done using the Transform.TransformDirection() method, which transforms the direction vector from world space to local space, ensuring that the scaling is performed relative to the object’s local axes rather than the world axes.

After the direction vector is transformed, the position of the object is adjusted to ensure that it stays centered during scaling. This is done by adding half of the scaled realDirection vector to the object’s position.

Finally, the scaling itself is performed by adding the unscaled direction vector multiplied by the desired scale amount to the object’s localScale.

public static class TransformExtensions {
        public static void ScaleInDirection(this Transform transform, Vector3 direction, float scaleAmount) {
            // Transform direction if you want to scale based on local rotation.
            Vector3 realDirection = transform.TransformDirection(direction);
            transform.position += realDirection * (scaleAmount * 0.5f);
            transform.localScale += direction * scaleAmount;
        }
    }

To use this method, simply call it on a Transform component, passing in the direction and scale amount parameters. For example, to scale an object in the positive X direction by a factor of 2, you could call:

transform.ScaleInDirection(Vector3.right, 2f);

This would scale the object in the positive X direction by a factor of 2, with the center of the object remaining in the same position.

Scale between two points

The method takes in two Vector3 parameters representing the two points between which the object should be scaled. The positions of these points are stored in pointA and pointB.

We will also need to remember the initial scale and store that in the variable startScale.

The distance between the two points is calculated using the Vector3.Distance() method. This value is then used to set the scale of the object along the x-axis to the distance between the two points. The y and z scales of the object are set to the initial y and z scales of the object respectively.

We then position the object at the center point between the two given points. This is done by calculating the average of the two points using the formula (pointA + pointB) * 0.5f. We assign this value to the transform.

The direction between the two points is calculated using the difference between pointB and pointA. The object is then rotated to face in the direction of the points using the transform.right. This might need to be changed depending on your object.

public static void ScaleBetweenPoints(this Transform transform, Vector3 pointA, Vector3 pointB) {
            Vector3 positionPointA = pointA;
            Vector3 positionPointB = pointB;
            Vector3 startScale = transform.localScale;

            float distanceBetweenPoints = Vector3.Distance(positionPointA, positionPointB);
            transform.localScale = new Vector3(distanceBetweenPoints, startScale.y, startScale.z);

            Vector3 centerPositionBetweenPoints = (positionPointA + positionPointB) * 0.5f;
            transform.position = centerPositionBetweenPoints;

            Vector3 directionBetweenPoints = positionPointB - positionPointA;
            // Direction based on what axis you want to scale on.
            transform.right = directionBetweenPoints;
        }

Leave a Reply

Your email address will not be published. Required fields are marked *