Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Awkwardnaut/8a8dafd6e8b01ee222c765deed6c1f14 to your computer and use it in GitHub Desktop.
Save Awkwardnaut/8a8dafd6e8b01ee222c765deed6c1f14 to your computer and use it in GitHub Desktop.
C# Unity set the center of mass of your rigidbodies
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class CenterOfMass : MonoBehaviour
{
public Vector3 _localCenterOfMass;
private void Awake()
{
SetCenterOfMass();
Destroy(this);
}
void SetCenterOfMass()
{
gameObject.GetComponent<Rigidbody>().centerOfMass = _localCenterOfMass;
}
private void OnDrawGizmosSelected()
{
Vector3 worldCenterOfMass = transform.TransformPoint(_localCenterOfMass);
Gizmos.color = Color.cyan;
Gizmos.DrawSphere(transform.TransformPoint(_localCenterOfMass), 0.1f);
Gizmos.DrawLine(worldCenterOfMass + Vector3.up, worldCenterOfMass - Vector3.up);
Gizmos.DrawLine(worldCenterOfMass + Vector3.forward, worldCenterOfMass - Vector3.forward);
Gizmos.DrawLine(worldCenterOfMass + Vector3.right, worldCenterOfMass - Vector3.right);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment