0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

UnityのRigidbody2Dのsimulatedを切ったらOnMouseDragできなくなった

Posted at

経緯

ある日、落ち物ゲームを開発していました

落とすオブジェクトを始めは画面上部に固定しておいて、「落とす」ボタンを押した時に落下を始める様にしました。この固定/落とす仕組みはRigidbody2D.simulatedを切り替えることによって実装していました。

その後、落とすオブジェクトをマウスドラッグ操作で移動させられるようにOnMouseDrag関数内に移動スクリプトを書きました。

すると、落とすオブジェクトが画面上部に固定されている最中はドラッグ移動が出来ず、「落とす」ボタンを押して以降、物理演算が有効になっている最中のみドラッグ移動が出来るようになりました

結論

物理演算を切った状態でドラッグしたい時はRigidbody2D.isKinematic = trueを使いましょう

なぜ

Rigidbody2D.simulated公式リファレンスには以下の様に書かれています

シミュレーションしない場合、アタッチされているすべての Collider2D や Joint2D は物理シミュレーションに組み込まれません。

またMonobehaviour.OnMouseDrag関数の公式リファレンスには以下の様に書かれています

OnMouseDrag はユーザーが GUIElement または Collider をマウスでクリックし、ドラッグしている間呼び出されます。

すなわち、Rigidbody2D.simulated = falseの時はコライダーが反応しなくなっているのでOnMouseDrag関数が呼ばれないと推測できます

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?