イラスト、モデリング、Unity、VR関連

unityとかblenderとかvr関連の作業メモ

Unity スクリプトからボーンを操作する

HumanBodyBones - Unity スクリプトリファレンス

Animator-GetBoneTransform - Unity スクリプトリファレンス

 

上記のスクリプトを使用すると、UnityのHumanoidBoneの各関節の参照となるTransformを取得出来るので、それを操作する。

 Update()ではなく、LateUpdate()で行う必要がある

 

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

public class BoneMove : MonoBehaviour{

  [SerializeField] Animator Anim;
  //操作したいHumanoidに付与してるAnimator
  [SerializeField] float x;
  [SerializeField] float y;
  [SerializeField] float z;
  Transform HandTF;

  void Start(){
    HandTF = Anim.GetBoneTransform(HumanBodyBones.RightHand);
  }

  void LateUpdate(){
    HandTF.rotation =  Quaternion.Euler(x,y,z);
  }
}

 仕組みが良く分かってないのだが、モデル全体が回転すると手首も回転してしまう。