Unity streching sprite gameobject to fit two positions.

Hello.

I have a sprite gameobject, which I want to scale by x axis in order to fit it between two points.

I have this script:

public void strechBetween(Vector2 point1, Vector2 point2)
{
    
    Vector3 scale = transform.localScale;
    scale.x = Vector3.Distance(point1, point2);
    transform.localScale = scale;
}

But it streches to much. Is there any other solutions?

I haven’t tested this but you are forgetting to factor in the size of the sprite in units. try this

public void strechBetween(Vector2 point1, Vector2 point2)
 {
     float spriteSize = GetComponent<SpriteRenderer>.sprite.rect.width / GetComponent<SpriteRenderer>.sprite.pixelsPerUnit;
     
     Vector3 scale = transform.localScale;
     scale.x = (point1.x - point2.x) / spriteSize;
     transform.localScale = scale;
 }