Changing the pivot point of meshes.

New to Unity and 3d graphics here so bear with me as I try to ask what likley amounts to a really silly question

I have a tank model who's turret I am trying to rotate, however its pivot point is not set correctly so the turret rotation seems off.

I know for transformation you can set the Pivot to center of the mesh or to the actual pivot point, but is it possible to change the pivot point ourselves inside Unity Editor?

It's yet not possible to set the pivot directly in the editor. Only way around is to attach your object to a new gameObject and set the your object's local transform in relation to its new parent so that the pivot gets moved.

This guy wrote an editor script to allow the user to edit an object's pivot location. Currently it's limited to within the object's bounding box, but he says this is fixable.

Link: http://solvethesystem.wordpress.com/2010/01/15/solving-the-pivot-problem-in-unity/

As far as I know, this is not possible within the editor. It may be possible to do this with scripting and in that case you might be able to write some sort of editor script that does this for you - but in general, I think the best solution for this is using a modelling tool. Given the smooth asset pipeline in Unity, that's usually "almost as efficient" as if you would be doing it inside Unity itself. And ... given that this is something that usually an artist would do, I think it's even more efficient to do it in the modelling tool (as the artist will usually know how to do it there).

I'm terrible at describing things, so I'll just give a code snippet. The long and short of it is, use transform.RotateAround function.

public class RotateAroundObject : MonoBehaviour {

private GameObject torret;
    private GameObject tankPivot;

private float rotateValue = 0;

Vector3 pivotTransVector;

// Use this for initialization
void Start () 
{
    torret = GameObject.Find("torret");
    tankPivot = GameObject.Find("tankPivot");

    pivotTransVector.x = tankPivot.transform.position.x;
    pivotTransVector.y = tankPivot.transform.position.y;
    pivotTransVector.z = tankPivot.transform.position.z;
}

// Update is called once per frame
void Update () 
{
            //Just an incremented value to rotate by
    rotateValue += Time.deltaTime;

            //This will rotate around the x, you would need to change this
            //to whatever axis the tank follows
    scriptText.transform.RotateAround(pivotTransVector, Vector3.right, rotateValue);
}

}

Hope this helps a little

It is not possible in the editor. Instead set an empty GameObject, and apply a box collider for that. And make that the parent object. And then try to rotate. But set the position of the object correctly. And adjust the scaling size to 1:1:1.

It may probably help you.

I looked everywhere on changing the pivot point in code. But I couldn’t find anything. So I started messing around and came up with this.
Thought I’d post it for those having the same problem.

public void Example()
{
	//Get the vertices from the gameObject
	Vector3[] objectVerts = myGameObject.GetComponent().mesh.vertices;
	//Initialize an offset
	Vector3 offset = Vector3.zero;
	//Loop through our vertices and add them to our offset
	for(int i = 0; i < objectVerts.Length; i++)
	{
		offset += objectVerts*;	*
  •   }*
    
  •   //Divide our offset by the amount of vertices in the gameObject (Getting the average)*
    
  •   offset = offset / objectVerts.Length;*
    
  •   //Loop through our vertices and subtract the offset*
    
  •   for(int i = 0; i < objectVerts.Length; i++)*
    
  •   {*
    

_ objectVerts -= offset;_
* }*
* //Assign the modified vertices to our gameObeject*
* myGameObject.GetComponent().mesh.vertices = objectVerts;*
* //Adjust the position of our gameObject to offset the vertices being offset. lol*
* myGameObject.transform.position += offset;*
* }*

I’m completely new to unity also, I was looking for answers for the same problem. I had my pivot set corectly in my 3d package but unity still was giving me a center pivot. Noticed below the unity menu, buttons that say center and local, clicked on center and it switched to the local pivot I set in my 3d package.

In this instance i’m only animating but if I was moving by code which pivot will the unity use? the center or the pivot? Is there anyway to switch between the two by code?

Wouaw awesome jedeo !!! It was impossible to find a real solution without your script !

I have converted it to javascript :

// On prend les sommets de l'objet :
var objectVerts : Vector3[] = GetComponent(MeshFilter).mesh.vertices;

//On initialise un offset
var offset : Vector3 = Vector3.zero;

// On fait une boucle sur les sommets et on les ajoute à l'offset :
for (i = 0; i < objectVerts.Length; i++)
{
offset += objectVerts*;*

}

// On divise l’offset par le nombre de sommets de l’objet (pour faire la moyenne)
offset = offset / objectVerts.Length;

// On fait une boucle sur nos sommets et on soustrait l’offset
for (i = 0; i < objectVerts.Length; i++)
{
objectVerts -= offset;
}

// On assigne les sommets modifiés à notre game object
GetComponent(MeshFilter).mesh.vertices = objectVerts;
// On ajuste la position de notre objet pour offseter les sommets devenus offsetés
transform.position += offset;
Seb

not the smartest way, but its works…

_ create an empty game object
_ drag it to position/rotation/… you prefer
_ set mesh/object as child

April 2016 and this is still not a proper feature. Good job unity developers, you definitely know your priorities…

easiest of easiest !!

  1. make a empty game object add the
    mesh in it
  2. boom it comes to center of mesh

Here’s another option for finding the center of an object to manipulate in scripts. This works fairly well for me, though it’s definitely going to be more reliable with symmetrical objects, and assumes that the rotation and scale of the object’s center are the same as the rotation and scale of the off-center transform/pivot:

Renderer myRenderer;
Vector3 actualCenter;

void Start(){

    //Store a reference to the renderer component of your object on start:
        Renderer myRenderer = GetComponent<Renderer>();
    
    //or slightly safer version of the same thing in case you have a mesh with some hierarchy 
    //(finds the first element in your object hierarchy that has a renderer):
        Renderer myRenderer = GetComponentInChildren<Renderer>();
    
}

void Update() {
    //get the latest 'bounds.center' Vector3 from your stored renderer reference:
        actualCenter = myRenderer.bounds.center;

   //do something with it.
}

Not ideal, but with some custom functions to just keep grabbing this bounds.center and offset your transformations by it, it serves most of the same purpose as a true centered pivot point.

Still, I agree it’s strange that there isn’t a more built-in, straightforward way to just center pivots, especially since the “Center/Pivot” toggle exists in the Editor and works very well in that context. That suggests to me that the internal code / computations for recentering pivots clearly exist within the guts of Unity, and just haven’t been exposed for this purpose.

Just wanted to update this with a simple solution that now exists since I was looking for the information as well and I just kept coming across the parent object work around. I stuck this in a couple other threads that pop up high on google to try to help out.

  • Probuilder, which is a unity asset right off the asset store, that I believe they said was
    going to become standard in the
    future, has the ability to set the
    pivot point of an object in editor.
  • When you have an object selected
    clicking on “Freeze Transform
    will set the pivot point of the
    object to 0,0,0 in world space while
    keeping the other transform
    information of your mesh exactly
    where it is in world space. So just
    take the object that you want to
    correct the pivot of to near 0,0,0,
    align it how you want and then click
    Freeze Transform. Very Handy, and
    officially supported by unity.
  • There is also a button to set the
    pivot to the meshes center of mass if
    you just want to do that quickly.
  • An added benefit of downloading probuilder is that you can edit meshes in unity as well, which is obviously great.

ProBuilder Package has this function from 2018.1+