Skip to content

Instantly share code, notes, and snippets.

@terokorp
Created July 30, 2023 16:50
Show Gist options
  • Save terokorp/3622e3cbc7bc1e2f4914f63f9ed78511 to your computer and use it in GitHub Desktop.
Save terokorp/3622e3cbc7bc1e2f4914f63f9ed78511 to your computer and use it in GitHub Desktop.
Move current object to its parents position
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Transform))]
public class MoveParentObjectEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
if (GUILayout.Button("Move Parent to Current Object's Position"))
{
MoveParentToCurrentPosition();
}
}
private void MoveParentToCurrentPosition()
{
Transform currentTransform = (Transform)target;
if (currentTransform.parent == null)
{
Debug.LogWarning("The object doesn't have a parent to move.");
return;
}
Vector3 parentPosition = currentTransform.parent.position;
foreach (Transform child in currentTransform.parent)
{
if (child != currentTransform)
{
child.position = child.position - currentTransform.position + parentPosition;
}
}
currentTransform.parent.position = currentTransform.position;
currentTransform.localPosition = Vector3.zero;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment