# How to scale a sprite on x axis to touch the point without changing pivot?

I have a sprite. The pivot is at random point.

I want to increase the sprite's scale on the x axis, without moving the pivot, until its edge touches a point at right.

I have a method to set/add size. I calculate a distance from the sprite's edge to the point at the right and add it as a size.

The problem is the sprite scale increases its size in both directions of the x axis (left and right), considering the pivot is not at the left edge of the sprite.

How can I scale the sprite while keeping the left edge where it is?

I have:

I want to get:

I solved the problem. The next method sticks a right side of a gameobject to the point on x axis:

ScaleToStick:

private void ScaleToStick(GameObject gameObject, Vector3 target)
{
Bounds bounds1 = StaticScript.GetBoundsChildren(gameObject);
float size = target.x - (bounds1.center.x + bounds1.extents.x);
float distancePivotToCenter = bounds1.center.x - transform.position.x;
float _1 = distancePivotToCenter + bounds1.extents.x;
float _2 = distancePivotToCenter - bounds1.extents.x;
_2 = Mathf.Abs(_2);
float x = (_1 + _2) / _1;
size *= x;

}


GetBoundsChildren:

public static Bounds GetBoundsChildren(GameObject gameObject)
{
Bounds bounds = new Bounds();
bool first = true;

foreach (Renderer renderer in gameObject.GetComponentsInChildren<Renderer>())
{
if (first)
{
first = false;

if (renderer.bounds.size == Vector3.zero)
{
Bounds bounds_ = new Bounds();
bounds_.center = renderer.gameObject.transform.position;
bounds = bounds_;
}
else
bounds = renderer.bounds;
}
else
if (renderer.bounds.size == Vector3.zero)
{
Bounds bounds_ = new Bounds();
bounds_.center = renderer.gameObject.transform.position;
bounds.Encapsulate(bounds_);
}
else
bounds.Encapsulate(renderer.bounds);
}

return bounds;
}


public static void AddSizeUniform(GameObject gameObject, float sizeAdd, string axis)
{
Vector3 scaleOld = gameObject.transform.localScale;
Vector3 sizeOld = GetBoundsChildren(gameObject).size;

Vector3 scaleNew = Vector3.zero;
float scaleNewMultiply = 0;

if (axis == "x")
scaleNewMultiply = (sizeOld.x + sizeAdd) / sizeOld.x;

if (axis == "y")
scaleNewMultiply = (sizeOld.y + sizeAdd) / sizeOld.y;

if (axis == "z")
scaleNewMultiply = (sizeOld.z + sizeAdd) / sizeOld.z;

scaleNew.x = scaleNewMultiply * scaleOld.x;
scaleNew.y = scaleNewMultiply * scaleOld.y;
scaleNew.z = scaleNewMultiply * scaleOld.z;

gameObject.transform.localScale = scaleNew;
}