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();
}
}
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();
}
}
Your answer
Follow this Question
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