LoginSignup
3
4

More than 3 years have passed since last update.

Fixed Joint機能について

Last updated at Posted at 2020-03-21

Fixed Jointについて

今回はFixed Jointの機能を使ってオブジェクト同士をくっつける方法を簡単に説明していきたいと思います。

今回は以下の2つのオブジェクトを使用して説明していきたいと思います.

bandicam 2020-03-17 16-10-05-677.jpg
bandicam 2020-03-17 20-33-22-708.jpgbandicam 2020-03-17 20-33-29-156.jpg

CubeにこんなScriptをつけてみました

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeScript : MonoBehaviour
{
    private float one = 0.05f;
    // Start is called before the first frame update
    void Start()
    {   
    }
    // Update is called once per frame
    void Update()
    {
        transform.position += new Vector3(one, 0, 0);
    }
}

すると,
ダウンロード1.gif

もちろんCapsuleは動かず,Cubeだけ進んでいきます

そこで使うのがFixed Jointです

 Fixed Jointの機能

bandicam 2020-03-17 20-48-39-112.jpg
- Connected Body:くっつけたいオブジェクトを入れる場所
- Break Force:くっつけたオブジェクトを離すために必要な力
- Break Torque:くっつけたオブジェクトを離すために必要なトルク
- Enable Collision:チェックをつけるとくっつけたオブジェクト同士が衝突をすることができる
- Enable Preprocessing:チェックをつけると不可能な動きをした時に安定した動きを取るようにする
- Mass Scale:質量の大きさ
- Connected Mass Scale:くっつけたオブジェクトの質量の大きさ

今回はScriptでFixed Jointの解除を行う予定なので,Connected BodyにCapsuleを入れるだけでOKです!

bandicam 2020-03-17 21-18-15-625.jpg

注意:くっつけるオブジェクトには必ずRigidBodyがついていることを確認してください

【物理的な物事を行う際はRigidbodyをつける】というUnityの鉄則があるので,今回もその鉄則にのっとり,必ずRigidbodyが両方についていることを確認してください

(つなげたいObjectにRigidbodyがついていないとFixed JointのConnected Bodyに入れることができないです)

Connected BodyにCapsuleを入れ,実行すると...

ダウンロード2.gif

このままだとCapsuleが引きずられている風になっているので,CubeのRigidbodyを開いて,ConstraintからFreeze Rotationnoのzにチェックを入れます

*bandicam 2020-03-17 15-54-55-578.jpg

Z軸のRotationを固定したので...

ダウンロード3.gif

これで完成です!

付記

if文とDestroyを使えば簡単にComponentの解除や追加をできるので,この機会にぜひFixed Jointを使ってみてください

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeScript : MonoBehaviour
{
    private float one = 0.05f;
    // Start is called before the first frame update
    void Start()
    {   
    }
    // Update is called once per frame
    void Update()
    {

        transform.position += new Vector3(one, 0, 0);
       if (Input.GetMouseButtonDown(0))
        {
            FixedJoint component = this.gameObject.GetComponent<FixedJoint>();
            Destroy(component);
        }

    }
}

ダウンロード4.gif

3
4
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
3
4