- Home /
 
 
               Question by 
               alexanderameye · Dec 10, 2016 at 04:55 PM · 
                rotationverticespivotpivot-pointvertice manipulation  
              
 
              Pivot modifying script
I have this script and the author couldn't make it work with rotations, and I've been stuck for hours trying to fix it. The problem is that the script can modify the pivot position when the object has rotation (0,0,0), but when it's rotating, not only the transform moves, but also the vertices move, and all in a weird way. Could someone help me?
 using UnityEngine;
 using UnityEditor;
  
 public class SetPivot : EditorWindow {
  
     Vector3 NewPivot; //New Pivot Value
     Vector3 OldPivot; //Last used Pivot Value
  
     GameObject obj; //Selected object in the Hierarchy
     MeshFilter meshFilter; //Mesh Filter of the selected object
     Mesh mesh; //Mesh of the selected object
     Collider col; //Collider of the selected object
 
     bool SharedMesh = false;
  
     bool PivotUnchanged;
  
   [MenuItem ("GameObject/Edit Pivot")]
   static void Init ()
     {
       SetPivot window = (SetPivot)EditorWindow.GetWindow (typeof (SetPivot));
             window.RecognizeSelectedObject();
       window.Show ();
   }
 
     void RecognizeSelectedObject()
     {
         //if(Selection.activeTransform)
         //{
         //obj = Selection.activeTransform.gameObject
         //}
         //else obj = null
         obj = Selection.activeTransform ? Selection.activeTransform.gameObject : null;
 
         if(obj)
         {
             meshFilter = obj.GetComponent(typeof(MeshFilter)) as MeshFilter;
             mesh = meshFilter ? meshFilter.sharedMesh : null;
 
             if(mesh)
             UpdatePivotVector();
             col = obj.GetComponent(typeof(Collider)) as Collider;
             PivotUnchanged = true; //UpdatePivotVector was called
         }
 
         else    mesh = null;
     }
 
     void OnSelectionChange()
     {
         RecognizeSelectedObject();
     }
 
     void OnGUI()
     {
         if(obj)
         {
             if(mesh)
             {
                 SharedMesh = EditorGUILayout.Toggle("Shared Mesh", SharedMesh);
 
                 EditorGUILayout.ObjectField("Selected gameobject: ", obj, typeof(SetPivot), true);
 
                 NewPivot.x = EditorGUILayout.Slider("X", NewPivot.x, -1.0f, 1.0f);
                 NewPivot.y = EditorGUILayout.Slider("Y", NewPivot.y, -1.0f, 1.0f);
                 NewPivot.z = EditorGUILayout.Slider("Z", NewPivot.z, -1.0f, 1.0f);
 
                 if(NewPivot != OldPivot) //if sliders were changed
                 {
                     UpdatePivot();
                     OldPivot = NewPivot;
 
                     //Only create instance of mesh when user changes pivot
                     if(PivotUnchanged)
                     {
                         mesh = (!SharedMesh) ? meshFilter.mesh : meshFilter.sharedMesh;
 
                       PivotUnchanged = false;
                  }
                 }
 
                 if(GUILayout.Button("Center")) //if button was pressed
                 {
                     NewPivot = Vector3.zero;
                     UpdatePivot();
                     OldPivot = NewPivot;
 
                     //Only create instance of mesh when user changes pivot
                     if(PivotUnchanged)
                     {
                         mesh = (!SharedMesh) ? meshFilter.mesh : meshFilter.sharedMesh;
 
                       PivotUnchanged = false;
                      }
                 }
 
                 GUILayout.Label("Center: " + mesh.bounds.center.ToString());
                 GUILayout.Label("Extents: " + mesh.bounds.extents.ToString());
             }
             else     EditorGUILayout.HelpBox("Selected object does not have a Mesh specified.", MessageType.Warning);
         }
         else EditorGUILayout.HelpBox("No object selected in Hierarchy.", MessageType.Warning);
     }
  
     //Achieve the movement of the pivot by moving the transform position in the specified direction
     //and then moving all vertices of the mesh in the opposite direction back to where they were in world-space
     void UpdatePivot()
     {
     
         Vector3 diff = (Vector3.Scale(mesh.bounds.extents, OldPivot - NewPivot)); //Calculate difference in 3d position
 
 Debug.Log("pos" + obj.transform.position);
 Debug.Log("diff" + diff);
 Debug.Log("scale" + obj.transform.localScale);
 
 obj.transform.position -= Vector3.Scale(diff, obj.transform.localScale); //Move object position
 
         //Iterate over all vertices and move them in the opposite direction of the object position movement
         Vector3[] verts = mesh.vertices;
         for(int i = 0 ; i < verts.Length ; i++)
         {
             verts[i] = new Vector3(verts[i].x + diff.x, verts[i].y + diff.y, verts[i].z + diff.z);
         //    += diff;
         //verts[i] = new Vector3(verts[i].x + NewPivot.x, verts[i].y + NewPivot.y, verts[i].z + NewPivot.z);
         }
         mesh.vertices = verts; //Assign the vertex array back to the mesh
 
 
         mesh.RecalculateBounds(); //Recalculate bounds of the mesh, for the renderer's sake
         //The 'center' parameter of certain colliders needs to be adjusted
         //when the transform position is modified
         if(col)
         {
             if(col is BoxCollider)
             {
                 ((BoxCollider) col).center += diff;
             }
 
             else if(col is CapsuleCollider)
             {
                 ((CapsuleCollider) col).center += diff;
             }
 
             else if(col is SphereCollider)
             {
                 ((SphereCollider) col).center += diff;
             }
         }
     }
  
     //Look at the object's transform position in comparison to the center of its mesh bounds
     //and calculate the pivot values for xyz
     void UpdatePivotVector()
     {
         Bounds bounds = mesh.bounds;
         Vector3 offset = -1 * bounds.center;
         NewPivot = OldPivot = new Vector3(offset.x / bounds.extents.x, offset.y / bounds.extents.y, offset.z / bounds.extents.z);
     }
  
 
 
 
 }
 
 
              
               Comment
              
 
               
              Your answer