BlenderのChildOf constraint相当をUnityで

メタバースとやらも意識しなければならず、やっぱり、Unityも勉強しておこうという気になってきた。Unityは1年半以上ご無沙汰で腕がかなり鈍っている。

 

Blenderで作った月面基地シーンのアニメーションをUnityへインポート。うまくいかないと思ったが、IKで作った複雑なロボットアームの動きも再現出来た。なるほど、Unityでつくるときも、複雑なアニメーションはBlenderで作ればいいんだ。

 

しかし、BlenderのChildOfを利用したアニメーションをUnityへインポートしたときは、アニメーションとしてうまく動作してくれない。よって、この部分だけはUnityのスクリプトで対処することにした。

 

荷物を挟んだ瞬間でアニメーションからイベントを発生させ、スクリプトでそれを受けて、荷物オブジェクトのtransform.parentを建設機械オブジェクトの一部に設定する処理。

 

Chopper

 


```

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChildOf : MonoBehaviour
{
    [SerializeField]
    GameObject m_CraneChopper;

    [SerializeField]
    GameObject m_BaseModule;

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
    }

    public void OnModuleHeld(string held)
    {
        Debug.Log("Module held");
        m_BaseModule.transform.parent = m_CraneChopper.transform;
    }

}

```