Scaling an object from a different center

I'm having a problem with the center of the localScale operation. Code is like this:

transform.localScale = new Vector3(transform.localScale.x*boundsSizeVelocity, transform.localScale.y*boundsSizeVelocity, transform.localScale.z*boundsSizeVelocity);

`boundsSizeVelocity` is a float value for the ratio which I want to scale by and is constantly changing.

All this works perfectly, except the center of the scale operation is always the center of the game object. I need to scale the object from an arbitrary position.

Is the some way of telling Unity what the center of my scale operation should be?

EDIT

Right, finally got round to implementing this. It has one major problem, my child object is rotating at the same time as scaling. Attaching the Empty GameObject makes the child dependant on the parent's rotation, which is not what I want.

The accepted answer is good, but if you want the math-y way with no parenting like I did, I referenced this page:
https://forum.unity.com/threads/scale-around-point-similar-to-rotate-around.232768/
(where I also posted my solution) to come up with this working function:

public void ScaleAround(GameObject target, Vector3 pivot, Vector3 newScale)
{
    Vector3 A = target.transform.localPosition;
    Vector3 B = pivot;

    Vector3 C = A - B; // diff from object pivot to desired pivot/origin

    float RS = newScale.x / target.transform.localScale.x; // relataive scale factor

    // calc final position post-scale
    Vector3 FP = B + C * RS;

    // finally, actually perform the scale/translation
    target.transform.localScale = newScale;
    target.transform.localPosition = FP;
}

Look on the forum link for more of my description on how to use it, and happy coding :slight_smile:

Parent your GameObject to a new empty GameObject. Then you can offset it relative to that empty object's position, and apply your scale values to the empty parent object.

Because your original gameobject is now a child, it's affected by the parent's scale, but the scale is applied relative to the parent's centre.

Hope this solves your problem!

Get your transform's world position. Translate your transform to the point you want to scale from. Scale. Translate NOT back to your old world position, but to what that old world position would be after the scaling (i.e. something like TransformPoint(myOldPosition)).

I’ve created a static method to scale the target around the pivot:

public static void ScaleAround(Transform target, Transform pivot, Vector3 scale) {
        Transform pivotParent = pivot.parent;
        Vector3 pivotPos = pivot.position;
        pivot.parent = target;        
        target.localScale = scale;
        target.position += pivotPos - pivot.position;
        pivot.parent = pivotParent;
    }