Unity pivot point presentation

Unity – Pivot Point

Pivot points are a concept found in Unity, the 3D, and 2D worlds. We will first delve into pivot points associated with sprites.
Next, we will handle the absence of a pivot point in GameObjects (3D).
Finally, we will discuss pivot points in the context of user interface (UI) and canvases.

Table of Contents

What is a Pivot?

A definition I like about the pivot is that on which everything else rests and turns.
In Unity, the pivot point serves as a reference for applying transformations.

Pivot in Sprite

How to Get Pivot Point in Sprite

To retrieve a sprite pivot point, call the pivot property:

using UnityEngine;

public class PivotTest : MonoBehaviour
{
    public Sprite sprite;

    private void Start()
    {
        var pivot = sprite.pivot;
    }
}

Change Pivot Point

Firstly, to modify a pivot point in a sprite, you need to install the “com.unity.2d.sprite” package in the PackageManager:

com.unity.2d.sprite package

Next, you can use the Unity editor interface.
Select your texture, then click on “Sprite Editor“.
Modify the pivot and click apply to save.

Unity sprite editor

For bulk modifications, you can use the following code:

using UnityEditor;
using UnityEngine;

public class PivotEditor : MonoBehaviour
{
    [MenuItem("Window/Change Pivot to Center")]
    public static void ChangePivotToCenter()
    {
        for (int i = 0; i < Selection.objects.Length; i++)
        {
            ChangePivot(Selection.objects[i] as Texture2D, SpriteAlignment.Center);
        }
    }

    private static void ChangePivot(Texture2D texture2D, SpriteAlignment spriteAlignment)
    {
        string path = AssetDatabase.GetAssetPath(texture2D);
        TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;

        TextureImporterSettings textureImporterSettings = new TextureImporterSettings();
        textureImporter.ReadTextureSettings(textureImporterSettings);
        textureImporterSettings.spriteAlignment = (int)spriteAlignment;

        textureImporter.SetTextureSettings(textureImporterSettings);
        textureImporter.SaveAndReimport();
    }
}

To use this example, select your textures and then click on the “Window” -> “Change Pivot to Center” button. All your textures will have a centered pivot point.

Change Sprite Pivot at Runtime

Changing the pivot point of a sprite at runtime using the method above is not recommended due to its performance issues. The preferred approach is to use other techniques, such as parenting.

using UnityEngine;

public class PivotTest : MonoBehaviour
{
    public SpriteRenderer spriteRenderer;

    private void Start()
    {
        ChangePivot();
    }

    private void ChangePivot()
    {
        var sprite = spriteRenderer.sprite;

        var newSprite = Sprite.Create(sprite.texture, sprite.rect, new Vector2(0.5f, 0.5f));
        spriteRenderer.sprite = newSprite;
    }
}

Change Pivot Point on UI

To change the pivot point via UI, you can directly modify the pivot values in the editor.
A refresher on how the UI world works in this article.

Unity UI pivot

You can also use the anchor presets as a shortcut.
You can hold down the Shift key to change the pivot in addition to the anchors.

Unity anchor presets

In addition, by holding ALT, the object’s position is directly modified.
The combination of the ALT and SHIFT key will modify the anchors, pivot, and position of the object, all three at the same time.

Alternatively, you can dynamically modify it in code:

var rectTransform = transform as RectTransform;
rectTransform.pivot = new Vector2(0.5f, 0.5f);

Change the Pivot Point of a 3D object.

In Unity, the pivot point for 3D objects is not directly modifiable. If you have imported a model with an external tool such as Blender, the recommendation is to set the pivot in the tool before importing the models into Unity.

To simulate a pivot effect, you can use parenting:

We will create a parent for our 3D object, positioning it in the exact location of our 3D object. Subsequently, we will simulate the pivot by altering the position of his child.

Unity 3d pivot

Pivot and Center

In Unity, the button at the bottom of “Window” in the toolbar allows you to switch between Pivot and Center. This button determines whether the display of the Gizmo is at the pivot or the center of the selected GameObject.

Unity center pivot

Note that during a multiple selection, if ‘pivot‘ is chosen, the Gizmo displayed will be that of the first selected game object.

Rotate Around Pivot

Rotating around the pivot point in Unity is achieved by simply applying a standard rotation. Since the position of the GameObject is relative to its pivot point, the regular rotation methods, such as Transform.Rotate, automatically rotate the object around its designated pivot.

// Rotate around Y axis with 30 degrees per second
transform.Rotate(Vector3.up * Time.deltaTime * 30f);

Unity pivot rotate

If you want to rotate around a different point, use RotateAround:

// Rotate around a custom point
transform.RotateAround(customPoint, Vector3.up, 30f * Time.deltaTime);

In conclusion, pivots play a crucial role in Unity, whether you are manipulating sprites, UIs, or 3d objects.
Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *