0
\$\begingroup\$

When I add component like RigidBody2D or BoxCollider, Can I set offset / size in scene window in Unity? Other than manually set value in inspector form, then look in scene window whether it is fit or not?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Yes. Press the Edit Collider button in the inspector.

Screenshot of using the Edit Collider button on a BoxCollider in Unity

Screenshot of using the Edit Collider button on a BoxCollider2D in Unity

This will add handles to the green frame representing the extents of the collider, and you can drag those to reposition & resize the collider visually.

Rigidbodies / Rigidbody2Ds don't have a concept of "size" or "offset" per se - these are computed from their attached colliders. So there's no built-in way to edit those at the Rigidbody level. If you want to override their center of mass though, and manipulate this visually, we can do this with a small helper script:

public class CenterOfMass : MonoBehaviour {
     void OnEnable() {
         var rb = GetComponentInParent<Rigidbody2D>();

         if(rb == null) {
             Debug.LogWarning("Use center of mass script only inside a Rigidbody2D");
             return;
         }

         rb.centerOfMass = rb.GetPoint(transform.position);
     }
}

Put this script on an empty GameObject and position it as a child of your Rigidbody2D-containing object. The position of this child will become the center of mass when the script is initialized.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .