0
1

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.

オブジェクトに乗っても落下しないようにする

Posted at

1.はじめに

スーパーマリオ・ロックマン等のアクションゲームにありがちな動く床。はじめての制作だとプレイヤー物に乗っても動く床に沿って動かなかったり、下のGif動画のように落ちることがある。
解決策はとても簡単なものだが、ネタとしてはアリだと思いアウトプットします。
ここだと床の上昇のみでの紹介になるが、この方法はそれ以外の動きにも対応が可能です。

なおここでは接触判定等の紹介はしません。

qiitaparents1.gif

2.スクリプト

スクリプトは単純明快で動く床の子オブジェクトになればいいです。注意点は動く床から離れた場合、親子関係を解除しないと動く床の挙動に影響され続けてしまいます。

using UnityEngine;

public class QiitaObject : MonoBehaviour
{

   //物体と接触したら物体の子オブジェクトとなる
   private void OnCollisionEnter(Collision collision)
   {
       transform.parent = collision.gameObject.transform;
   }

   //物体から離れたら物体の子オブジェクトを解除する。
   private void OnCollisionExit(Collision collision)
   {
       transform.parent = null;
   }
}

qiitaparents2.gif

3.最後に

親子関係の扱いはVR制作だと物を掴む等で使っていましたが、それ以外の用途では使うことがなかったので、とても勉強になった。
これ以外にももっと良い方法があるかもしれませんが、その時は加筆をしたいと思います。

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?