I’ve been beating my head against the wall for a week now off and on trying to figure this out. I want the same functionality you get with the tool in Unity, but at runtime for a level editor I’m working on.
private void OnMouseDown() {
pointerStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
sizeStart = myTool.myEditingObject.GetComponent<SpriteRenderer>().bounds.size;
scaleStart = myTool.myEditingObject.transform.localScale;
positionStart = myTool.myEditingObject.transform.localPosition;
}
private void OnMouseDrag() {
if (scaleSide == ScaleSide.RIGHT) {
pointerTravel = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - pointerStart;
scaleX = ((sizeStart.x + pointerTravel.x) / sizeStart.x) * scaleStart.x;
positionX = positionStart.x + (pointerTravel.x / 2);
var scale = new Vector2(scaleX, myTool.myEditingObject.localScale.y);
var pos = new Vector2(positionX, myTool.myEditingObject.localPosition.y);
myTool.Scale(scale, pos); //Just applies the above math to the object the math is meant for.
}
This code works perfectly… that is, until you rotate the object. Then it falls apart.
There is a gif of the problem.
Basically, I need to figure out the math to move against the mouse on the Y axis like I do for X to compensate for the new downward motion when the object is rotated. Unfortunately, myself and everyone I’ve talked to in a few gamedev Discord’s can’t seem to get it to work just right.
Any help appreciated, thanks!