使用帧中的刻度函数根据相机位置更新对象位置

Using tick function in a-frame to update object position based on camera position

我正在使用 A-Frame 并希望对象随相机移动。为此,我将一个组件放在一起,根据相机位置更新对象的位置:

      AFRAME.registerComponent('follow', {
    schema: {
      distance: {type: 'vec3'},
      target: {type: 'selector'}
    },

    tick: function() {
      const targetItem = this.data.target;
      const relativePosition = this.data.distance

      var tempPos = targetItem.getAttribute("position").split(" ").map(Number);

      targetPos = new THREE.Vector3(relativePosition.x + tempPos[0], relativePosition.y + tempPos[1], relativePosition.z + tempPos[2]);

      this.el.setAttribute('position', targetPos)
    }
  });

当我使用 init 而不是 tick 时,这工作正常,但由于它是一个初始化函数,它只在场景开始时更新一次。出于某种原因,当我使用 tick 时,一切都崩溃了。我用错了吗?我应该做些不同的事情来不断更新它的位置吗?

提前致谢!

编辑: 我应该提一下,目标是让一些东西随处可见,但 不是 固定在您的视图中。想想《时之笛》中的 Navi。

从外部收到解决方案SO:

我需要将我的 WASD 控制器放在包含相机的实体上:

<a-entity id="character" position="0 2 3"  wasd-controls look-controls>
  <a-entity id="camera" camera>
    <a-ring radius-outer="0.03" radius-inner="0.02"
            position="0 0 -1"
            material="color: cyan; shader: flat"
            cursor="fuse: true; fuseTimeout: 500">
    </a-ring>
    </a-camera>
</a-entity>

然后,我需要更改函数:

 tick() {
          const targetItem = this.data.target;
          var distance = this.data.distance;
          var targetPosition = targetItem.getAttribute('position');
          this.el.setAttribute('position',{
            x: targetPosition.x + distance.x,
            y: targetPosition.y + distance.y,
            z: targetPosition.z + distance.z
          });

然后成功了!