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.

プレイ中にFreezePositionとFreezeRotationのオンオフ

Posted at

空中アスレチックを作成していて、回る物体を作りました。
回してPlayerを移動させるとPlayerが物体に当たったら下の床に食い込むんですね。
この原因はPlayerのRigidbodyのFreezePositionのY軸のチェックが入ってなかったからです。
では、チェックを入れるとどうなるのか?
現象は解消されました!
が。。。床から落とすと当然下に落ちません。。。
こういう場合、床に乗っている時はFreezePositionのY軸をオン、
床に乗っていない時はオフにしたい訳です。

まず、RigidbodyのFreezePositionとFreezeRotationを全てチェックを外します。
それからコードを書いていきます。

rb.constraints = RigidbodyConstraints.FreezeRotation;  //Rotationを全てオン
rb.constraints = RigidbodyConstraints.FreezePosition;  //Positionを全てオン
rb.constraints = RigidbodyConstraints.FreezeRotationX;  //RotationのXのみオン
rb.constraints = RigidbodyConstraints.FreezeRotationY;  //RotationのYのみオン
rb.constraints = RigidbodyConstraints.FreezeRotationZ;  //RotationのZのみオン
rb.constraints = RigidbodyConstraints.FreezePositionX;  //PositionのXのみオン
rb.constraints = RigidbodyConstraints.FreezePositionY;  //PositionのYのみオン
rb.constraints = RigidbodyConstraints.FreezePositionZ;  //PositionのZのみオン

※rbはRigidbodyの変数です。
では、私の場合はRotationは全てオン、PositionはY軸のみオンにしたいので。。。

rb.constraints = RigidbodyConstraints.FreezeRotation;  //Rotationを全てオン
rb.constraints = RigidbodyConstraints.FreezePositionY;  //PositionのYのみオン

こう書けば大丈夫!!
だと思いましたが、残念ながら違います…

rb.constraints = RigidbodyConstraints.FreezeRotation  //Rotationを全てオン
            | RigidbodyConstraints.FreezePositionY;  //PositionのYのみオン

こう書かないといけないのです。
1行目はセミコロンのみ書かなくて良くてrb.constraintsは書かなくていいです!!
これで大丈夫!!

ちなみに
私の場合は常にRotationは全てオン、
床に乗っている時だけPositionはY軸のみオンにしたいので。。。

//collisionに接触している場合はRotationは全てオン、PositionはY軸のみオンにする。
void OnCollisionStay(Collision collision)
    {
        rb.constraints = RigidbodyConstraints.FreezeRotation
            | RigidbodyConstraints.FreezePositionY;
    }
//collisionに接触してない時はPositionのY軸はオフ。Rotationは全てオンのまま。
    void OnCollisionExit()
    {
        rb.constraints = RigidbodyConstraints.FreezeRotation;
    }

これで最初は全てチェック外している状態ですが、Rotationは全て、PositionのY軸のみプレイしたらチェックが入り、床から落ちるとPositionのY軸のみチェックが外れます!!

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?