- Home /
The question is answered, right answer was accepted
Is there a way to draw center of mass on the screen?
Hi everyone,
I'm trying to see where the center of mass is located on my rigidbody, is there a way to draw it on the screen so I can then position it accordingly?
Thanks in advance!
Answer by ScroodgeM · Aug 29, 2012 at 09:32 PM
http://docs.unity3d.com/Documentation/ScriptReference/Editor.html
RigidbodyEditor.cs
using UnityEditor; using UnityEngine; [CustomEditor(typeof(Rigidbody))] public class RigidbodyEditor : Editor { void OnSceneGUI() { Rigidbody rb = target as Rigidbody; Handles.color = Color.red; Handles.SphereCap(1, rb.transform.TransformPoint(rb.centerOfMass), rb.rotation, 1f); } public override void OnInspectorGUI() { GUI.skin = EditorGUIUtility.GetBuiltinSkin(UnityEditor.EditorSkin.Inspector); DrawDefaultInspector(); } }
Thanks for taking the time to write this! When I add it to Assets/Editor I get
"Instance of RigidbodyEditor couldn't be created because there is no script with that name."
Oh never$$anonymous$$d I missed a case letter, playing with your code further now - will get back to you :)
Thanks so much this worked perfectly, for anyone that is interested in the Javascript version
@CustomEditor(typeof(Rigidbody)) public class RigidBodyEditor extends Editor { function OnSceneGUI() : void { var rb : Rigidbody = target as Rigidbody; Handles.color = Color.red; Handles.SphereCap(1, rb.transform.TransformPoint(rb.centerOf$$anonymous$$ass), rb.rotation, 0.1f); } override function OnInspectorGUI(): void { GUI.skin = EditorGUIUtility.GetBuiltinSkin(UnityEditor.EditorSkin.Inspector); DrawDefaultInspector(); } }
Note: you have to click on the Gameobject to see the Handle.
Thanks again!
It does work great but there's a side-effect (Unity 5) : the 'Constraints' section of the Rigidbody in the Inspector disappears. Any ideas on how to make it visible again ? Thanks !
This duplicates all the other fields but does not bring back the Constraints
Ins$$anonymous$$d of posting a "thank you" comment of a 4+ years old answer (which bumps the question for no reason) you should simply upvote the answer (and question). That's enough. I'm going to lock the comments to prevent further comment of this kind.
Follow this Question
Related Questions
Character Controller children of a car: Inertia effect, delay effect, what??? 1 Answer
Character controller jittering up and down 2 Answers
Problem with Rigidbody.CenterOfMass 2 Answers
How can I rotate around with a Rigidbody while respecting physics? 0 Answers
AddRelativeTorque() Applied Incorrectly When Using a Custom Centre of Gravity 0 Answers