- Home /
How to stretch a sprite without distortion ?
What is the best way to go about this? What I'm trying to do, is to stretch the object along the X axis while the finger is touching the screen and when the finger is lifted the object falls down. The problem is that my script works fine when a solid color is applied (red, white, black, etc. ), but when I try to import some custom sprites, for example planks, the sprite quality distorts, because of stretching. Is there a way to maintain the same quality when the sprite is stretched ? I think the way to go about this is to use solid color while it is stretching and after the finger is released apply the sprite, but I have no idea how to achieve this.
Script:
public class Extend : MonoBehaviour
{
public float speed = 0.2f;
Rigidbody2D rb;
public GameObject line;
bool TouchInputOff = false;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
if (!TouchInputOff)
{
if (Input.GetMouseButton(0))
{
line.transform.localScale = new Vector3(transform.localScale.x + 0.2f * speed, transform.localScale.y, 0);
}
if (Input.GetMouseButtonUp(0) || line.transform.localScale.x >= 5.0f)
{
rb.gravityScale = 3;
TouchInputOff = true;
}
}
else
{
rb.gravityScale = 3;
}
}
}
Answer by tuinal · Jul 14, 2020 at 02:19 PM
You need to set up the sprite for 9-slicing, since what you effectively want is the middle part to tile, and the corners to stay as they are. Unity can do this for you in the Sprite Editor, but you need a sprite with a central texture that's tileable for a good visual end-result.
https://docs.unity3d.com/Manual/9SliceSprites.html
Answer by sachinchetu · Jul 20, 2020 at 11:37 AM
Yes, you can cut sprite in three-part and whenever you need to stretch it just add middle part in your sprite, don’t change the order of these sprites –Starting part > Middle part > Ending Part and it will work for you like this- Starting part > Middle part1 > Middle part2>…….. >Middle partN> Ending Part. @AarnasS75