Address
304 North Cardinal St.
Dorchester Center, MA 02124

Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM

Unity pivot point presentation

Unity – Pivot Point

Pivot points are a concept found in both Unity and the 3D (2D too) worlds. In this article, we will first delve into pivot points associated with sprites.
Next, we will explore how to address 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: 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’s 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

To modify a pivot point in a sprite: you’ll first need to install the “com.unity.2d.sprite” package in the PackageManager:

com.unity.2d.sprite package

After 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, simply 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.
If you need a refresher on how the UI world works, check this article.

Unity UI pivot

You can also use the anchor presets to do this, by holding 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. It is possible to hold ALT+SHIFT to modify the anchors, pivot and position 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 Pivot Point of 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, it is recommended 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 at the same location as our 3D object. Subsequently, we will simulate the pivot by altering the position of the 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 Gizmo is displayed at the pivot or the center of the selected GameObject.

Unity center pivot

Note that during a multiple selection, if ‘pivot‘ is chosen, the pivot of the first selected gameObject will be utilized.

Rotate Around Pivot

Rotating around the pivot point in Unity is achieved by simply applying a standard rotation. Since the GameObject is already positioned relative to its pivot point, using the regular rotation methods, such as Transform.Rotate, automatically rotates 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’re manipulating sprites, UIs or 3d objects.
Happy coding !

Check my other articles about Unity:

Leave a Reply

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