I am currently utilizing A-Frame and aiming to make an object move along with the camera. In order to achieve this, I have created a component that constantly updates the object's position based on the camera's position:
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)
}
});
While using init
instead of tick
works fine, it only updates once at the beginning of the scene. Strangely, everything breaks when I switch to using tick
. Am I implementing it incorrectly? Is there a different approach to consistently update its position?
Thank you in advance!
Edit: I want to clarify that the objective is to have something follow the camera around without being fixed to your view - similar to Navi from Ocarina of Time.