我想调整只有一个对象的Y坐标,同时保持X和Z坐标相同。我工作的唯一方法是获取对象的位置并组成一个新的部分修改的位置。这不太好,似乎可能效率低下。看起来这可能是由于position is a single-property component这个事实,所以也许这是不可能的。如何设置一些对象的坐标而不重置其他坐标?
这是我目前的解决方案:
https://glitch.com/edit/#!/aframe-position-example
的index.html:
<html>
<head>
<script src="https://aframe.io/releases/0.6.0/aframe.min.js"></script>
</head>
<body>
<a-scene>
<a-camera id="camera"></a-camera>
</a-scene>
<script src="index.js"></script>
</body>
</html>
index.js:
const camera = document.querySelector('#camera')
console.log(camera.getAttribute('position'))
// {x: 0, y: 1.6, z: 0}
// overwrites original position, as expected
camera.setAttribute('position', { x: 0, y: 0, z: 5 })
console.log(camera.getAttribute('position'))
// {x: 0, y: 0, z: 5}
// overwrites original position (including z, defaults to 0), maybe not expected
camera.setAttribute('position', { x: 5, y: 5 })
console.log(camera.getAttribute('position'))
// {x: 5, y: 5, z: 0}
// overwrites original position (x and z become 0), maybe not expected
camera.setAttribute('position', 'y', 10)
console.log(camera.getAttribute('position'))
// {x: 0, y: 10, z: 0}
// how to change some position variables and keep the other ones the same
let oldPos = camera.getAttribute('position')
let newPos = { x: 4, y: oldPos.y, z: oldPos.z }
camera.setAttribute('position', newPos)
console.log(camera.getAttribute('position'))
// {x: 4, y: 10, z: 0}
不会改变底层Three.js位置后来弄乱了什么吗?如果我在箱子上做'getAttribute('position')',它不是旧的位置吗? 而第二种解决方案是我目前正在做的,看起来可能是唯一的方法。 – boxtrain
@jbjw yup,正如我写的,我通常使用'临时变量'选项来保持位置,我已经更新了我的anwser以避免混淆,如果你正在寻找一个干净的班轮,使用Rainers的想法,我仍然希望将参考中的位置对象作为一个整体。 –