0
\$\begingroup\$

I am using Unity. In my game, I have an SpriteRenderer and I'd like it to increase its size

Screenshot of the sprite

I'd like it to lengthen to the right, without moving the left edge.

I tried changing its scale to something bigger than its original scale and it changes into something like this, growing to both the left and the right:

Screenshot of widened sprite, showing it has grown both left & right

I also tried to increase its position together with its scale but then it didn't work.

Editor's note: this is where you tell us how it "didn't work". What exactly did you try, what results did you get, and how did those results differ from what you wanted?

My Object is configured like this in UnityEngine:

Inspector screenshot

\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

The pivot of your object is at it's center. So to keep the bottom left corner in the same position, you need to move the object up and to the right by:

(bigSize / 2.0) - (originalSize / 2.0)

Alternatively, just put a parent gameObject at the corner, and child your GameObject to it, and apply the scaling to the parent instead.

\$\endgroup\$
5
  • \$\begingroup\$ I have just tried that alternative method. I added a parent gameObject and scaled it. But I got no clue, how to return it's position after it moves to certain place. \$\endgroup\$ Jan 3, 2019 at 14:14
  • \$\begingroup\$ transform.position or transform.localPosition \$\endgroup\$
    – Evorlor
    Jan 3, 2019 at 14:33
  • \$\begingroup\$ I tried and set my scale to be bigger. So, how do we set the condition to make the scale smaller to it's previous size? \$\endgroup\$ Jan 3, 2019 at 14:53
  • \$\begingroup\$ I'm sorry, but I don't understand. Can you clarify? If you mean revert to it's old size, just same the scale before resizing, and then set it back. \$\endgroup\$
    – Evorlor
    Jan 3, 2019 at 16:31
  • \$\begingroup\$ Exactly.When I enlarge the image by increasing it's scale, it Should end up something like "3.08" on x-axis. When it reaches the maximum point i.e. 3.08 , it should again come to it's original scale i.e. 1. The problem is: I have enlarged the image and when I try to reset it's position by if statement, I couldn't do it. I wrote if(theScale > MaximumScale) "theScale--"; Here, the problem was that when the Scale was big enough , it ran this part of code and made the scale 1 pixel smaller. Now, when the pixel is 1 pixel smaller than the Maximum scale , it cannot run this part of code anymore. \$\endgroup\$ Jan 5, 2019 at 3:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .