I'm currently trying to spawn an object in case there's an intersection between a laser object and a collidable object. I'm using a raycaster to detect the collision.
To spawn the object I want to do it only in the case there's a collision and also the user has pressed the trigger button.
I was thinking on creating a global variable pressed when the triggerdown event listener was pressed and in the raycaster-intersection event listener spawn an object only if this variable was set to true.
const pressed = false
AFRAME.registerComponent('laser', {
init: function () {
const laser = this.el.sceneEl.querySelector('#laser');
laser.addEventListener('raycaster-intersection', function(event) {
console.log('raycaster-intersection', event.detail.intersections[0].point)
if (pressed) {
console.log('spawn')
}
});
}
})
AFRAME.registerComponent('spawner', {
init: function () {
const blockHand = this.el.sceneEl.querySelector('#blockHand');
blockHand.addEventListener('triggerdown', function(event) {
pressed = true
});
blockHand.addEventListener('triggerup', function(event) {
pressed = false
});
}
})
I don't like to use global variables but I don't know how could I tackle this problem without them in this case.
Any suggestions?
Thanks!