Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//途中省略
const tmpRay = new BABYLON.Ray();
tmpRay.origin = new BABYLON.Vector3();
tmpRay.direction = new BABYLON.Vector3();
tmpRay.length = 3;// rayが届く範囲を3に制限。
var hit;
var tmpMesh;
//途中省略
xrHelper.input.onControllerAddedObservable.add((controller) => {
controller.onMotionControllerInitObservable.add((motionController) => {
if (motionController.handness === 'left') {
const xr_ids = motionController.getComponentIds();
let triggerComponent = motionController.getComponent(xr_ids[0]);//xr-standard-trigger
triggerComponent.onButtonStateChangedObservable.add(() => {
if(triggerComponent.value > 0.5){//trigger-buttonを押した量が0.5以上だったら (Max=1)
controller.getWorldPointerRayToRef(tmpRay, true); // コントローラからrayを飛ばす
hit = scene.pickWithRay(tmpRay);
if (hit.pickedMesh !=undefined){// rayが当たった対象をhit.pickedMeshに格納
tmpMesh = hit.pickedMesh;
console.log("name:"+hit.pickedMesh.name);
//tmpMesh.parent= controller.grip;//tmpMesh is set on inappropriate position.
tmpMesh.setParent(motionController.rootMesh);//rayが当たったmeshをコントローラの子オブジェクトにする
}
//released button
}else if (triggerComponent.value < 0.5){
if (tmpMesh !=undefined){
// tmpMesh.parent= null;
tmpMesh.setParent(null);//ボタンを離したらコントローラの子オブジェクトを解除
}
}
});
}
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment