Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

70 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Rotate Object around point only 90 Degree. 1 Answer

Real pivot rotation of all vertices in a mesh? 2 Answers

How do I rotate in the center after changing the position of the pivot? 1 Answer

How do you change the pivot point of a group? 1 Answer

Freeze object rotation on Polybrush 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges