# Unity Stretch Sprite between two Points at runtime

How to stretch a Sprite between two points say initialPosition , FinalPosition in world coordinates, Please elaborate with code, Thanks

using UnityEngine;
using System.Collections;

``````public class Player : MonoBehaviour {
public Vector3 startPosition = new Vector3(0f,0f,0f);
public Vector3 endPosition = new Vector3(5f, 1f, 0f);
public bool mirrorZ = true;
void Start() {
Strech(gameObject, startPosition, endPosition, mirrorZ);
}

public void Strech(GameObject _sprite,Vector3 _initialPosition, Vector3 _finalPosition, bool _mirrorZ) {
Vector3 centerPos = (_initialPosition + _finalPosition) / 2f;
_sprite.transform.position = centerPos;
Vector3 direction = _finalPosition - _initialPosition;
direction = Vector3.Normalize(direction);
_sprite.transform.right = direction;
if (_mirrorZ) _sprite.transform.right *= -1f;
Vector3 scale = new Vector3(1,1,1);
scale.x = Vector3.Distance(_initialPosition, _finalPosition);
_sprite.transform.localScale = scale;
}
}
``````

It would be difficult to do it from the sprite, From what I have used with sprites you could change the scale, rotation and position and such to make it fit between two points but using to points to find that would be a little trickier. (I suppose if you wanted to do the math you could derive your own rotation and scales from the two points.)

If a GUI is an option you could use this:

``````GUI.DrawTexture (new Rect (<X1>, <Y2>, <X1>-<X2>, <Y1>-<y2>), MySprite.texture, ScaleMode.ScaleToFit);
``````

Hope that helps.
Cheers,
BM