Check out this drag example: https://codepen.io/alexcheninfo/pen/vKkgkE. You'll notice that if you stack cubes and drag the one in front, the one behind it moves as well.
Take a look at the complete code below:
<script>
AFRAME.registerComponent('draggable', {
init() {
this.mouse = new THREE.Vector2();
this.scene = this.el.sceneEl;
this.camera = this.scene.camera;
this.obj = this.el.object3D;
this.scene.addEventListener('mousemove', e => {
this.mouse.x = (e.offsetX / this.scene.canvas.offsetWidth) * 2 - 1;
this.mouse.y = -(e.offsetY / this.scene.canvas.offsetHeight) * 2 + 1;
if (this.selected) {
let r = new THREE.Raycaster();
r.setFromCamera(this.mouse, this.camera);
let dist = this.obj.position.distanceTo(this.camera.position);
let point = r.ray.direction.multiplyScalar(dist);
this.el.setAttribute('position', `${point.x} ${point.y} ${point.z}`);
}
});
this.scene.addEventListener('mousedown', e => {
let r = new THREE.Raycaster();
r.setFromCamera(this.mouse, this.camera);
let intersected = r.intersectObject(this.el.object3D, true);
let objPos = this.el.object3D.position;
let camPos = this.camera.position;
console.log(objPos.distanceTo(camPos));
if (intersected.length) this.selected = true;
});
this.scene.addEventListener('mouseup', e => {
this.selected = undefined;
});
}
});
</script>
<a-scene>
<a-entity camera look-controls></a-entity>
<a-sky src="https://raw.githubusercontent.com/aframevr/aframe/master/examples/boilerplate/panorama/puydesancy.jpg"></a-sky>
<a-box color="tomato" position="-3 0 -10" draggable></a-box>
<a-box draggable position="3 0 -5" draggable></a-box>
</a-scene>
Any suggestions on how to prevent the cubes from moving together? For example, restricting movement to only the cube in the front?