How can I move the transform gizmo? I have a box with two parts – a lid and a bottom. I want to rotate the lid, but want the rotation to be centered on an edge of the box.
You are talking about the Scene View in edit mode, right?
Sadly, it is not possible to free move the transform gizmo or so much the pivot of an object in Unity by default. You can choose your pivot to be either Center or Pivot and choose the transform handle to be local or global, but that’s it.
For the easiest workaround: Create an empty GameObject at the position of the hinges. Then drag your lid onto that object, making it a child. Now, when you rotate the empty gameobject, all child objects will follow and keep their offset. So basically you create your own custom pivot by parenting an empty object.
This is an old question but if it can help anyone, I created this script and so far it works ok. Might not be bug free though ^^
- You select all the items you want to move in the scene
- Then keep your mouse pointer where you want the new gizmo to be
- press the shortcut (we have many shortcuts so I just threw that in there)
- Now you should have a gizmo selected which you can move, which should move the other items, unless they are under influence with some other constraints.
- Hitting the shortcut again will destroy the new gizmo and remove the parent constraints on the selected objects.
public class SetTransformGizmo : UnityEditor.Editor
{
private static GameObject _currentTempParent;
private static Object[] _movingObjects;
private static ParentConstraint[] _createdConstraint;
[MenuItem("Tools/MyTool/Toggle Gizmo %#&x")]
static void DoIt()
{
if (Selection.objects.Length == 0) return;
if (_currentTempParent != null)
{
foreach (var constraint in _createdConstraint)
{
constraint.constraintActive = false;
DestroyImmediate(constraint);
}
DestroyImmediate(_currentTempParent);
_createdConstraint = new ParentConstraint[0];
_currentTempParent = null;
Selection.objects = _movingObjects;
_movingObjects = null;
}
else
{
Vector2 mousePos = Event.current.mousePosition;// (0,0) is upper left
mousePos *= UnityEditor.EditorGUIUtility.pixelsPerPoint;// OS display scale
var camera = UnityEditor.SceneView.lastActiveSceneView.camera;
Vector3 vec = new Vector3(mousePos.x, camera.pixelHeight - mousePos.y, 1);// (0,0) is lower left
Ray ray = camera.ScreenPointToRay(vec);
if (new Plane(inNormal: Vector3.up, inPoint: Vector3.zero).Raycast(ray, out float dist))
{
Vector3 hitPoint = ray.origin + ray.direction * dist;
_movingObjects = Selection.objects;
_currentTempParent = new GameObject("-- PARENT GIZMO --");
SceneManager.MoveGameObjectToScene(_currentTempParent, SceneManager.GetSceneAt(1));
_currentTempParent.transform.position = hitPoint;
_createdConstraint = new ParentConstraint[_movingObjects.Length];
for (int i = 0; i < _createdConstraint.Length; i++)
{
ParentConstraint parentConstraint = _movingObjects[i].AddComponent<ParentConstraint>();
_createdConstraint[i] = parentConstraint;
parentConstraint.AddSource(new ConstraintSource() { sourceTransform = _currentTempParent.transform, weight = 1f });
Transform sourceTransform = _currentTempParent.transform;
Vector3 positionOffset = sourceTransform.InverseTransformPoint(((GameObject)_movingObjects[i]).transform.position);
Quaternion rotationOffset = Quaternion.Inverse(sourceTransform.rotation) * ((GameObject)_movingObjects[i]).transform.rotation;
parentConstraint.SetTranslationOffset(0, positionOffset);
parentConstraint.SetRotationOffset(0, rotationOffset.eulerAngles);
parentConstraint.weight = 1;
parentConstraint.locked = true;
parentConstraint.constraintActive = true;
}
Selection.activeTransform = _currentTempParent.transform;
}
else
{
Debug.LogError("RAYCAST FAILED");
}
}
}
}