我想調整隻有一個對象的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的想法,我仍然希望將參考中的位置對象作爲一個整體。 –