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?
1 Answer
Yes. Press the Edit Collider button in the inspector.
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.