- Home /
How to drag-and-stretch a 2D sprite in local space?
I'm making a drag-and-stretch 2D sprite. Below is a simplified version of the code I have. It allows you to click the sprite and the scale and position will adjust to keep the right side directly below the pointer as you drag. This works perfectly.
void OnMouseDown()
{
_pointerStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
_sizeStart = GetComponent<Renderer>().bounds.size;
_scaleStart = transform.localScale;
_positionStart = transform.position;
}
void OnMouseDrag()
{
var _pointerTravel = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - _pointerStart;
var _scaleX = ((_sizeStart.x + _pointerTravel.x) / _sizeStart.x) * _scaleStart.x;
transform.localScale = new Vector3(_scaleX, transform.localScale.y, transform.localScale.z);
var _positionX = _positionStart.x + (_pointerTravel.x / 2);
transform.position = new Vector3(_positionX, transform.position.y, transform.position.z);
}
That is, it works perfectly when it's not rotated. I know to work with it rotated, I need to translate the whole thing into local space. But here I'm stumped. I've tried a hundred permutations, but I can't quite get it. Here's one of the many broken versions.
Can anyone lend me some guidance on what I need to change?
Thanks!
void OnMouseDown()
{
_pointerStart = transform.InverseTransformPoint(Camera.main.ScreenToWorldPoint(Input.mousePosition));
_sizeStart = transform.InverseTransformVector(GetComponent<Renderer>().bounds.size);
_scaleStart = transform.localScale;
_positionStart = transform.localPosition;
}
void OnMouseDrag()
{
var _pointerTravel = (Vector2)transform.InverseTransformPoint(Camera.main.ScreenToWorldPoint(Input.mousePosition)) - _pointerStart;
var _scaleX = ((_sizeStart.x + _pointerTravel.x) / _sizeStart.x) * _scaleStart.x;
transform.localScale = new Vector3(_scaleX, transform.localScale.y, transform.localScale.z);
var _positionX = _positionStart.x + (_pointerTravel.x / 2);
transform.localPosition = new Vector3(_positionX, transform.localPosition.y, transform.localPosition.z);
}
Your answer
Follow this Question
Related Questions
[2d]inversing scale modify position 0 Answers
Position + Vector3 doesn't return correct values 1 Answer
How can I move an object to a (0,0) and scale it to (0,0) at the same time? 1 Answer
Scale/Resize gameobject's y axis based off drag (help) 0 Answers
How do I permanently resize a model with children? 2 Answers