Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9703

Fixed Joint機能について

$
0
0

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がついていることを確認してください)

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


Viewing all articles
Browse latest Browse all 9703

Trending Articles