Skip to content

Instantly share code, notes, and snippets.

@noahjames404
Created April 16, 2025 02:21
Show Gist options
  • Select an option

  • Save noahjames404/f14a17fd8d6415eee289726970ed36d5 to your computer and use it in GitHub Desktop.

Select an option

Save noahjames404/f14a17fd8d6415eee289726970ed36d5 to your computer and use it in GitHub Desktop.
Centralize the game object pivot in unity
using Sirenix.OdinInspector;
using UnityEngine;
namespace maphatar.civsim
{
public class GetCenterBounds : MonoBehaviour
{
[SerializeField, ReadOnly]
Vector3 center;
[SerializeField]
GameObject reference;
[Button]
public void ComputeBoundsAndCentralized()
{
center = reference.transform.TransformPoint(reference.GetComponent<BoxCollider>().center);
var obj = new GameObject();
obj.transform.position = center;
transform.SetParent(obj.transform);
obj.name = name + " (container)";
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.yellow;
Gizmos.DrawSphere(center,1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment