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 /
  • Help Room /
avatar image
0
Question by Gr33nIguana · Apr 27, 2016 at 02:57 AM · rotationmovementeditorwindowsliderlocalposition

How can I move Object over local axis with sliders

I have made a small editor window with 2 game object fields and 3 sliders. I want to center / allign object 1 (Pivot object) with object 2 (Door Object) The button "Reset pivot to object" does that. Then I want to use the sliders x, y, and z, to move object 1 (Pivot object) over it's local axis, and constraint to the size of object 2. This also works, sort of...:-( When I enable the sliders, object 1 will get an offset. When object 2 has zero rotations, the script works fine, but the moment object 2 is rotated, object 1 will also get an offset. I can't seem to see why /or find a solution. I hope somebody can point me in the right way.

 using UnityEngine;
 using UnityEditor;
 
 public class SliderTest : EditorWindow
 {
     float myFloat = 0f;
     float x_axis = 0f;
     float y_axis = 0f;
     float z_axis = 0f;
     public Transform pivotObject = null;
     public Transform doorObject = null;
     Vector3 pointObjectSpace = new Vector3(0f, 0f, 0f);
     Vector3 pointWorldSpace = new Vector3(0f, 0f, 0f);
     bool changePivot = false;
 
     float minX = 0f;
     float maxX = 0f;
     float minY = 0f;
     float maxY = 0f;
     float minZ = 0f;
     float maxZ = 0f;
 
     [MenuItem("Example/Slider Test")]
     static void Init()
     {
         // Get existing open window or if none, make a new one:
         SliderTest window = (SliderTest)EditorWindow.GetWindow(typeof(SliderTest));
         window.Show();
     }
 
     void SetPositionToDoor()
     {
         pivotObject.position = doorObject.localPosition;
         pivotObject.rotation = doorObject.localRotation;
 
         pointWorldSpace = pivotObject.position;
         pointObjectSpace = pivotObject.TransformDirection(pointWorldSpace);
 
         x_axis = pointObjectSpace.x;
         y_axis = pointObjectSpace.y;
         z_axis = pointObjectSpace.z;
 
         minX = (doorObject.position.x - (doorObject.localScale.x / 2));
         maxX = (doorObject.position.x + (doorObject.localScale.x / 2));
         minY = (doorObject.position.y - (doorObject.localScale.y / 2));
         maxY = (doorObject.position.y + (doorObject.localScale.y / 2));
         minZ = (doorObject.position.z - (doorObject.localScale.z / 2));
         maxZ = (doorObject.position.z + (doorObject.localScale.z / 2));
     }
 
     void OnGUI()
     {
         EditorGUILayout.BeginVertical();
         GUILayout.Label("Base Settings", EditorStyles.boldLabel);
         doorObject = EditorGUILayout.ObjectField("Door:",doorObject, typeof(Transform), true) as Transform;
         pivotObject = EditorGUILayout.ObjectField("Pivot:", pivotObject, typeof(Transform), true) as Transform;
         pointWorldSpace = EditorGUILayout.Vector3Field("Point world space :", pointWorldSpace);
         pointObjectSpace = EditorGUILayout.Vector3Field("Point object space :", pointObjectSpace);
 
         if (pivotObject && doorObject)
         {
 
             if (GUILayout.Button("Reset pivot to object"))
             {
                 SetPositionToDoor();
             }
 
             if (GUILayout.Button("Edit Pivot"))
             {
                 changePivot = !changePivot;
             }
 
             GUI.enabled = changePivot;
             x_axis = EditorGUILayout.Slider("Slider X", x_axis, minX, maxX);
             y_axis = EditorGUILayout.Slider("Slider Y", y_axis, minY, maxY);
             z_axis = EditorGUILayout.Slider("Slider Z", z_axis, minZ, maxZ);
             GUI.enabled = true;
 
             if (changePivot)
             {
                 pointWorldSpace.x = x_axis;
                 pointWorldSpace.y = y_axis;
                 pointWorldSpace.z = z_axis;
                 pointObjectSpace = pivotObject.TransformDirection(pointWorldSpace);
               //pointObjectSpace = pivotObject.InverseTransformDirection(pointWorldSpace);
                 pivotObject.position = pointObjectSpace;
             }
         }
         EditorGUILayout.EndVertical();
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Gr33nIguana · Apr 27, 2016 at 11:09 PM

I found the solution :-D I will post it here for other people with similar problems. My answer was not in TransformDirection but with TransformPoint. Still not 100% sure if this is the best way to do it, but for now it works ;-)

If anybody has an idea of improving it, let me know.

 using UnityEngine;
 using UnityEditor;
 
 public class SliderTest : EditorWindow
 {
     //Pivot transform that we want to move relative to door, no matter door's orientation.
     public Transform pivotObject = null;
     //Door transform used to align pivot.
     public Transform doorObject = null;
 
     //Enable pivot control settings, enable set new pivot locattion.
     private bool changePivot = false;
 
     //Floats to control the X, Y, and Z sliders.
     private float x_axis = 0f;
     private float y_axis = 0f;
     private float z_axis = 0f;
 
     //Because we work relative to our door, we can set min / max movement to + / - half of object size.
     private float minX = -0.5f;
     private float maxX = 0.5f;
     private float minY = -0.5f;
     private float maxY = 0.5f;
     private float minZ = -0.5f;
     private float maxZ = 0.5f;
 
     //Create Menu button in example menu called slider test
     [MenuItem("Example/Slider Test")]
     static void Init()
     {
         // Get existing open window or if none, make a new one:
         SliderTest window = (SliderTest)EditorWindow.GetWindow(typeof(SliderTest));
         window.Show();
     }
 
     //Use this to repaint the editor window constantly.
     void OnInspectorUpdate()
     {
         this.Repaint();
     }
 
     void OnGUI()
     {
         EditorGUILayout.BeginVertical();
         GUILayout.Label("Base Settings", EditorStyles.boldLabel);
         EditorGUILayout.Space();
         EditorGUILayout.Space();
         //Create input fields to load in door and pivot objects.
         doorObject = EditorGUILayout.ObjectField("Door:",doorObject, typeof(Transform), true) as Transform;
         pivotObject = EditorGUILayout.ObjectField("Pivot:", pivotObject, typeof(Transform), true) as Transform;
         EditorGUILayout.Space();
         //Only show buttons and settings if both door and pivot objects have been loaded.
         if (pivotObject && doorObject)
         {
             //GUI.enabled controls if GUI objects are active or grey and is controlled by boolean
             //If we are not in pivot mode, this will grey out buttons and settings.
             GUI.enabled = !changePivot;
             //If this button is pressed, the pivot model will be aligned with the door.
             if (GUILayout.Button("Reset pivot to object"))
             {
                 //Get location and rotation of the door and pass them on to the pivot object.
                 pivotObject.position = doorObject.localPosition;
                 pivotObject.rotation = doorObject.localRotation;
                 //Reset the X, Y and Z sliders, so the pivot stay's / starts in the center.
                 x_axis = 0f;
                 y_axis = 0f;
                 z_axis = 0f;
             }
             GUI.enabled = true;
             //Button to enable / disable pivot editing mode
             if (GUILayout.Button("Edit Pivot"))
             {
                 changePivot = !changePivot;
             }
 
             GUI.enabled = changePivot;
             //Read the slider values.
             x_axis = EditorGUILayout.Slider("Slider X", x_axis, minX, maxX);
             y_axis = EditorGUILayout.Slider("Slider Y", y_axis, minY, maxY);
             z_axis = EditorGUILayout.Slider("Slider Z", z_axis, minZ, maxZ);
             GUI.enabled = true;
 
             //if we are in pivot edit mode
             if (changePivot)
             {
                 //create a new Vector3 with the sliders as input
                 Vector3 relativePos = new Vector3(x_axis, y_axis, z_axis);
                 //create a new Vector3 and fill it with the relativPos transformed into worldspace.
                 Vector3 exactPos = doorObject.TransformPoint(relativePos);
                 //Update the pivot position with the new world position relative to the door :-).
                 pivotObject.position = exactPos;
             }
         }
         EditorGUILayout.EndVertical();
     }
 }
Comment
Add comment · Share
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

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

i am using a code to make my cube walk with character controller ,but when i play the cube spins. 0 Answers

when my character rotates the controls of the initial rotation stay and not change with it 0 Answers

try to rotate a character controller when translating on z only 0 Answers

How to simply make this kind of movement? 0 Answers

Moving forward based off of Rotation 3 Answers


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