Question About raycasts

I would like to shoot a ray from a box that’s moving.
Am I supposed to create a ray:
var ray = new BABYLON.Ray(origin,direction,length);
for every frame ?
Is there a way to have the ray updates with the mesh.

1 Like

Hi @aekiro

Yes, you have to update the ray. You can do it before rendering for example, using

https://doc.babylonjs.com/api/classes/babylon.scene#registerbeforerender

2 Likes

Docs:
https://doc.babylonjs.com/babylon101/raycasts

Example:
https://www.babylonjs-playground.com/#ZHDBJ#48

2 Likes

Thanks. Your example is done with rayhelper though which is different.
I tried to do it with just the ray() method, But it does not seem to work:
https://www.babylonjs-playground.com/#ZHDBJ#51
Can you please have a look at what’s wrong in my code ?

1 Like

the ray direction wasn’t good.

https://www.babylonjs-playground.com/#ZHDBJ#52

2 Likes