How to rotate a game object pivoting by the center, and not by the corner?

Note that I’m using Unity UI to help visualize what I’m doing.

This is how the arrow rotates when just applying a new Quaternion value to its local rotation:

And this is what I plan to understand Quaternion rotations by adding an offset, in order to get this result (not yet happening at the moment):

This is my pseudo-code that doesn’t seem to work like I expected:

Transform transform = GetComponent<Transform>();
transform.position += new Vector3(arrow.GetWidth() / 2f, arrow.GetHeight() / 2f, 0f);
transform.localRotation = Quaternion.Euler(0f, 0f, angle) * Vector3.one;
transform.position -= new Vector3(arrow.GetWidth() / 2f, arrow.GetHeight() / 2f, 0f);

How do you rotate a game object by offsetting its pivot point (non Unity UI wise), like the green arrow in the second GIF?

You could use RotateAround

transform.rotation = Quaternion.identity;
transform.RotateAround(transform.position + new Vector3(arrow.GetWidth() / 2f, arrow.GetHeight() / 2f, 0f), Vector3.forward, angle);

]

You have three options.

  1. RotateAround
  2. Fix the pivot of your mesh in 3d tool
  3. Make mesh a child of gameobject and rotate parent.
2 Likes

Thank you.

Where can I look up Quaternion math involving rotating game objects? It’s okay if there’s no answer.

Third solution is so simple and makes the job done! Thanks!

1 Like

https://eater.net/quaternions

1 Like