Created
June 20, 2020 13:33
-
-
Save Awkwardnaut/8a8dafd6e8b01ee222c765deed6c1f14 to your computer and use it in GitHub Desktop.
C# Unity set the center of mass of your rigidbodies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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