0
\$\begingroup\$

I have a GameObject with a Sprite Renderer on it. The Pivot of the Sprite itself is Bottom Left (this can not be changed).

I created a parent to this GameObject, and want to grow and shrink the Scale of this Parent... so that the Sprite Renderer on the child appears to grow and shrink around the center of the sprite. I put a Rect Transform on the parent and set the X and Y pivot to 0.5.

But when I change the Scale of the Paremt gameobject, the Sprite Renderer appears to grow and shrink around the bottom left of the sprite (not the center).

How do I fix this?

\$\endgroup\$
4
  • \$\begingroup\$ "I put a Rect Transform on the parent" AFAIK RectTransform's are only used for objects on a canvas. Does that mean you are talking about a sprite that lives on a canvas, and not a sprite that lives in regular world space? Or did you somehow manage to put a RectTransform on a regular world space game object? \$\endgroup\$ Commented Apr 26, 2023 at 15:03
  • \$\begingroup\$ I've been using Rect Transforms without a canvas... apparently Text Mesh Pro implements what is effectively a world space canvas for its Rect Transform and anything nested under it. I'm making a word game, so many of my objects use TMP. In this case, I can take the Rect Transform off of the parent if that will help! \$\endgroup\$ Commented Apr 27, 2023 at 0:53
  • \$\begingroup\$ Note: I wrote code to switch the Sprite to one that's Centered instead of Bottom Left, and adjusted the local position of the GameObject with the Sprite Renderer to compensate. This works perfectly (the Sprite doesn't appear to move) but when I increase/decrease the Scale of the parent object... the sprite appears to grow/shrink from the bottom left and not the center. (After this grow/shrink stuff is done, I can easily change the Sprite Renderer component to once again use the Bottom Left sprite.) \$\endgroup\$ Commented Apr 28, 2023 at 12:38
  • \$\begingroup\$ Ah ha, if I change the scale of the child gameobject... it grows and shrinks around the center of the sprite! Not sure why that doesn't work if I change the scale of the parent gameobject... \$\endgroup\$ Commented Apr 28, 2023 at 12:48

1 Answer 1

0
\$\begingroup\$

I ended up creating two versions of the Sprite: Sprite 1 had the pivot at Bottom Left, and Sprite 2 had the pivot at the Center.

Then I did the following:

  1. I made the GameObject's Sprite Renderer switch from Sprite 1 to Sprite 2 (with a centered Pivot).
  2. I wrote a function which moved the GameObject position a bit algorithmically to compensate for the change in Pivot location.

Then I was able to grow and shrink the Scale of the GameObject, and the Sprite appeared to grow/shrink from the center of the object.

However I only wanted this to be a temporary change. So I also wrote code to reverse the above changes, so that the GameObject was once again using Sprite 1.

\$\endgroup\$
1
  • \$\begingroup\$ This answer would be even better if it showed the code you used, which can help future readers trying to implement a similar solution. \$\endgroup\$ Commented May 12, 2023 at 13:44

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.