Selecting nearest mesh when looking at it

Hi there:

I’m trying to figure out how to select/pick the nearest to camera mesh when looking at it by means of a Universal Camera. That is, picking such a mesh when is shown at the center of the screen.

I supposed that this would be a very common scenario and so there were some documentation or even several PGs on it, but couldn’t find any.

Any help on this?

As always, thanks for your time.

Thanks for your time.

Probably this section may help - Mesh Picking | Babylon.js Documentation

1 Like

Thanks, @labris:

As I understand, In order to set the ray cast we’re going to use to pick the meshes, an origin point and direction vector are both needed.

For my case of use, camera.position is of course the origin, and I’m using camera.getDirection in order to retrieve the direction.

This said, sadly the call to getDirection throws a “TypeError: can’t access property “_x”, e is undefined”.

You can take a look at all that through this PG, which of course shows only a very initial stage of the intended full behavior, namely, how to pick the nearest mesh when looking at it.

Thanks for your time.

getDirection requires a parameter

you could use getForwardRay which has optional parameters

Hope this helps.

2 Likes

Thanks @labris and @JohnK

The easy way to go is using camera.getForwardRay.

In this manner it works as needed, as you can see in this PG.

Best regards.

2 Likes