ゲームオブジェクトに Rigidbody 2D コンポーネントをアタッチすると、そのゲームオブジェクトを物理演算システムで制御することができます。Rigidbody 2D は、標準の Rigidbody と同じようなプロパティを持っていますが、2D 開発用に適応されています。例えば、Rigidbody 2D コンポーネントがアタッチされたゲームオブジェクトは、XY 平面に沿ってのみ移動でき、XY 平面に対して垂直な軸でのみ回転できます。
Unity エディターの Transform コンポーネントは、ゲームオブジェクト (およびその子ゲームオブジェクト) をシーン内でどのように配置、回転、スケールするかを定義します。このコンポーネントを変更すると、他のコンポーネントが更新され、それがレンダリングされる場所や他のコライダーの位置に影響を与えます。Unity の 2D 物理演算システムは、コライダーを動かし、コライダー同士を相互に作用させることができます。このために Unity は、物理演算システムがコライダーの動きを Transform コンポーネントに伝え返すための方法を必要とします。Rigidbody 2D コンポーネントは、コライダーに関わるこの動きと接続のために存在します。Rigidbody 2D コンポーネントは、Transform コンポーネントをオーバーライドして、位置や角度を Rigidbody 2D コンポーネントの定義するものに更新します。
ノート: Transform コンポーネントを直接修正して Rigidbody 2D をオーバーライドすることも可能です (Unity は全てのコンポーネントの全てのプロパティを公開しているため)。ただし、この場合、予測できない動きが発生したり、ゲームオブジェクト同士が互いを貫通するなどの問題が発生します。
同じゲームオブジェクトや子ゲームオブジェクトに加えた全ての Collider 2D コンポーネントは、その Rigidbody 2D ゲームオブジェクトに暗示的にアタッチされるため、Rigidbody 2D が動くと Collider 2D も一緒に動きます。アタッチされている Collider 2D を Transform やコライダーオフセットを使用して直接動かすことはせず、代わりに Rigidbody 2D を動かしてください。Rigidbody 2D を動かすことで、最良のパフォーマンスが得られ、正しい衝突判定が可能になります。同じ Rigidbody 2D にアタッチされた Collider 2D 同士は互いに衝突しません。つまり、1 つの合成コライダーとして効率的に動く一式のコライダー (全てのコライダーが Rigidbody 2D と同期して移動/回転する) を作成することが可能です。
Rigidbody 2D を追加すると、スクリプティング API から力を適用することによってスプライトの動きを物理的にリアリスティックなものにできます。適切なコライダーコンポーネントがアタッチされたスプライトゲームオブジェクトは、他の動くゲームオブジェクトとの衝突の影響を受けます。Unity の物理演算システムを使用することで、多くの一般的なゲームプレイメカニクスが単純化され、最小限のコーディングでリアルな動作表現が可能になります。
ノート: Rigidbody 2D は互いに衝突するものとして一般的に説明されますが、実際に衝突するのは各ボディにアタッチされた Collider 2D です。Rigidbody 2D はコライダーがなければ互いに衝突できません。
Rigidbody2D
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.