Trail lags 1 frame behind

I am using a trail mesh in a game, but I found that the trail lags behind one frame. I made a playground example to illustrate my point Babylon.js Playground. Just uncomment the lines in the animate function to see how the trail seperates from the mesh as it gets faster. How can I make the mesh stick to the object without having to calculate where it is in the next frame? Is this a bug and if not then what am I doing wrong?

There are two things:

  • the trail update function is using the generator mesh (the cube in the PG) world matrix for its computation. But because this update function is called in scene.onBeforeRenderObservable, the cube world matrix is not up to date yet: you have to call it yourself
  • in autoStart mode, it is the trail constructor that is adding the call to the update function in scene.onBeforeRenderObservable, else it is when you call TrailMesh.start. So:
    • if using autoStart = true, you must register your observer in scene.onBeforeRenderObservable (that will call cube.computeWorldMatrix()) before creating the trail mesh
    • if using autoStart = false, you can register your observer in scene.onBeforeRenderObservable whenever you want, as long as it is before calling TrailMesh.start.

I used the latter method as it is a bit more convenient:

https://www.babylonjs-playground.com/#ALW8PF#2

Hey thank you kindly. That fixes it.