Please click inside the Playground scene to activate pointer lock.
Controls:
WASD: Movement
Space: Jump
Click: Fire bullet
When a bullet mesh is created in front of your player, a Ray and RayHelper are attached to it. The first 1 or 2 pickWithRay's register false positive hits on the red slide (even if your player is far away from the slide or not even facing it). Subsequent pickWithRay's correctly and no longer register these hits against the slide.
For debugging, the bullets are stationary, and the code below is for debugging:
I can’t wait to see what you come up with after all of those experiments
So - here is the issue - check the log here - https://www.babylonjs-playground.com/#3EDS3A#235 , and check what I am doing in line 430. The thing is - the console is updated with new values after the old values were used for the calculation (as it is only a reference, and not a new object). It seems like the ray helper requires a frame to get the values correctly, but the playground is a bit too complex for me to dive in and understand why this happens. If you want to simplify it, we can investigate why this happens.
Using a setTimeout after 1 second, a bullet is created with a cyan Ray and RayHelper. The console.log usually registers 1 (or usually 2) hits against the pillar in scene.registerBeforeRender before no longer registering hits.
The world matrix of the bullter is not yet calculated when the ray helper is triggered and uses its worldMatrix to calculate the ray’s origin (and direction). This is done to save a few CPU cycles, but can be solved by adding compute world matrix when attaching the mesh (on our side). I will add this call later, in the meantime you can simply update the bullet world matrix before creating the ray helper
Added q.normalize() on Line 518. Without it, it gave the bullet’s rotationQuaternion very high xyzw values, which caused initial xyz values of the Ray's direction to be massive (as shown in above image).
Not sure if this is a new bug (Ray attached to mesh with unnormalized rotationQuaternion via RayHelper initially has incorrect direction xyz values but then corrects itself in subsequent frames/physics time steps).
Very thankful the Playground is now working. Thank you so much for your help, @RaananW!