トマシープが学ぶ

Unity/VR/AR/デザイン好きのミーハー 記事内容は自分用のメモです

【OculusHandTracking】物理&物をつかむなど【Unity】

物をつかみます

f:id:bibinbaleo:20191224120924p:plain

前回

bibinbaleo.hatenablog.com

OVRSkelton

EnablePhysicsCapsules=物理

OVRSkeltonのEnablePhysicsCapsulesにチェックを入れると手に物理が効きます。

f:id:bibinbaleo:20191224081000p:plain

できた!

f:id:bibinbaleo:20191224082510p:plain

ただトラッキングが消えてメッシュが消えるとすとんと落ちてしまう><

 LeapMotionはそんなことなかったのに。

UpdateRootScale=手の大きさ

あとUpdateRootScaleはその人の手の大きさによって手のモデルの大きさが変わるそうです!プレゼンス!

手のマテリアル

SkinnedMeshRendererにマテリアルをセットしたら変わります。

f:id:bibinbaleo:20191224082005p:plain

f:id:bibinbaleo:20191224114339p:plain

手にコライダー追従

RightControllerAnchorの下にオブジェクトを置いたら手に追従します。

f:id:bibinbaleo:20191224082255p:plain

000に置いたら手首らへんに表示されました

f:id:bibinbaleo:20191224082504p:plain

 

関節の位置を取得

こちらを参考に指先の位置を取得しました。

qiita.com

人差し指の位置だったらこんな感じ

Vector3 indexTipPos = MYRightSkelton.Bones[(int)OVRSkeleton.BoneId.Hand_IndexTip].Transform.position;

Quaternion indexTipRotate = MYRightSkelton.Bones[(int)OVRSkeleton.BoneId.Hand_IndexTip].Transform.rotation;

球をこの位置にしたらちゃんと指先に表示されました。

IndexSphere.transform.position = indexTipPos;

IndexSphere.transform.rotation = indexTipRotate;

f:id:bibinbaleo:20191224114339p:plain

つかむ

さっき作った指先の球コライダーと任意のオブジェクトがOnTriggerStayしていて、かつ親指がどこかの指とくっついているときに、対象のオブジェクトを指先の子にした。

つかんだ後は対象のオブジェクトをisKinematicにする。

離したらfalse。

スクリプト

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

public class Grab : MonoBehaviour
{
    [SerializeField] OVRHand MYRightHand;
    [SerializeField] OVRSkeleton MYRightSkelton;
    [SerializeField] GameObject IndexSphere;
    private bool isIndexPinching;
    private float ThumbPinchStrength;

    void Update()
    {
        isIndexPinching = MYRightHand.GetFingerIsPinching(OVRHand.HandFinger.Index);

        ThumbPinchStrength = MYRightHand.GetFingerPinchStrength(OVRHand.HandFinger.Thumb);

        Vector3 indexTipPos = MYRightSkelton.Bones[(int)OVRSkeleton.BoneId.Hand_IndexTip].Transform.position;
        Quaternion indexTipRotate = MYRightSkelton.Bones[(int)OVRSkeleton.BoneId.Hand_IndexTip].Transform.rotation;
        IndexSphere.transform.position = indexTipPos;
        IndexSphere.transform.rotation = indexTipRotate;
    }

    void OnTriggerStay(Collider other)
    {

        if (ThumbPinchStrength>0.9)///つかんだ
        {
            other.gameObject.transform.parent = IndexSphere.transform;
            other.GetComponent<Rigidbody>().isKinematic = true;
            other.gameObject.transform.localPosition = Vector3.zero;

        }
        else///はなした
        {
            other.GetComponent<Rigidbody>().isKinematic = false;
            other.transform.parent = null;

        }
    }

離すときにコライダーが接触して少し飛んで行っちゃう。

最後に

刺身たんぽぽのつかみはきれいだな~

全然違うやり方でやっているか、細かい調整があるんだろうな