首页 > 解决方案 > 如何根据相机使网格只能在 x 和 y 方向拖动?

问题描述

我希望我的子网格只能在相对于相机的 x 和 y 方向上拖动。我正在使用巴比伦的免费相机。尝试使用“Left Right Dragger”和“DragPlaneNormal”,但未能达到预期的效果,网格可在 z 方向拖动。

const axis = new BABYLON.Vector3(0,1,0);

const leftRightDragger = new BABYLON.PointerDragBehavior({dragAxis: axis});
leftRightDragger.moveAttached = true;
clone_child[child].addBehavior(leftRightDragger);

or

var pointerDragBehavior = new BABYLON.PointerDragBehavior({dragPlaneNormal: new BABYLON.Vector3(1,1,0)});
clone_child[child].addBehavior(pointerDragBehavior);
pointerDragBehavior.useObjectOrientationForDragging = false;

标签: draggablemeshbabylonjs

解决方案


使用 babylonjs 生命周期方法。

const mesh // the mesh now dragging
const zPos = mesh.position.z

scene.registerBeforeRender(() => {
  mesh.position.z = zPos
})


推荐阅读