D Park
D Park

Reputation: 514

What is the best way to extract component information that is intersected with raycaster?

Is there any way to extract the entity's information(id, class, etc) that is intersected with a raycaster? I tried to find the information from evt.detail.el but no success.

AFRAME.registerComponent('collider-check', {

init: function () {
  this.el.addEventListener('raycaster-intersected', function (evt) {
    console.log(evt.detail.el);
  });
 }
});

Upvotes: 0

Views: 57

Answers (1)

Dan S.
Dan S.

Reputation: 331

The issue is that the code above logs the raycasting entity, rather than the raycasted entity, so it is logging the cursor.

Using the code above, you can access the data you need by logging evt.detail.intersection.object.el. So you could do something like the following to access id and class, respectively:

console.log(evt.detail.intersection.object.el.id);
console.log(evt.detail.intersection.object.el.className);

Here is a demo of the code in action: https://codepen.io/dansinni/pen/bjjbWv

If you haven't bound this to the handler, and only need basic attribute data, you should also be able to do the following, but YMMV:

console.log(this.id);
console.log(this.className);

You should also be able to use a component with the cursor instead, and rely on the raycaster-intersection event. Note the difference in the docs: https://aframe.io/docs/master/components/raycaster.html#events

Upvotes: 1

Related Questions