Olivier Pons
Olivier Pons

Reputation: 15816

Re-calculate position after rotation

Let's say I have a UI Image like this:

+-+     Width = 128
|A|     Height = 640
|B|     Anchors (0,0,0,0)
|C|     Pivot (0,0)
|D|     Rotation (0,0,0)
*-+     Scale (1,1,1)

Note the * is the pivot point, and it's on bottom-left. I want to rotate this UI Image with rotation z = -90.0f. When I do this I have a box like this:

       Width = 128
       Height = 640
       Anchors (0,0,0,0)
       Pivot (0,0)
       Rotation (0,0,-90)
*----+ Scale (1,1,1)
|DCBA|
+----+

Note the * is the pivot point, and not it's not on bottom-left anymore, it's on top-left. Everything is logical from a 3D / maths point of view... but not for the 2D / Pixel guy I am. I have two problems with that:

Here's how I'd like the final position to be:

       Width = 640
       Height = 128
       Anchors (0,0,0,0)
+----+ Pivot (0,0)
|DCBA| Rotation (0,0,0)
*----+ Scale (1,1,1)

It's on the "click" event. Here's my code:

public void OnPointerClick(PointerEventData eventData)
{
    if (MenuMain.ModeRotateIsActivated) {
        eventData.pointerPress.transform.Rotate(new Vector3(0f, 0f, -90f));
        /* maybe use / change rt after that?
        RectTransform boat_rt = 
            eventData.pointerPress.GetComponent<RectTransform>();
        */
    }
}

What can I could do to modify the object position + pivot like I want?

Upvotes: 0

Views: 604

Answers (1)

You can't get what you want, because you want to:

  • Rotate the image
  • Translate the image
  • Update the pivot
  • Have the inspector read no rotation, no translation, pivot at 0

Each of the first three are mutually exclusive with the fourth.

At best you can perform the first three actions, then parent the image to an otherwise empty GameObject that is located on the correct corner and has no rotation applied to it. But it won't be the same object, it'll be a parent object.

Upvotes: 1

Related Questions