ArcRotateCamera setting lockedTarget once vs. setting target every frame in scene.registerBeforeRender()

Hi all! Noticed that setting the target of ArcRotateCamera every frame in scene.registerBeforeRender(), the player’s mesh experiences “abrupt” movement. When replacing this with setting lockedTarget just once, the movement became smooth. Could you please view the Playgrounds below, and I would be grateful for any insight on this.

In case the GIF’s below are unclear:

For the Playgrounds below:
Please click inside the Playground’s scene to activate pointer lock
Controls:
WASD: Movement
SPACE: Jump

1. Playground setting lockedTarget once - smooth movement :smiley: :

https://www.babylonjs-playground.com/#3EDS3A#71

camera.lockedTarget = cameraTargetMesh;

vs.

2. Playground setting target every frame in scene.registerBeforeRender() - abrupt movement :cry: :

https://www.babylonjs-playground.com/#3EDS3A#70

scene.registerBeforeRender(() => {
    // Update camera target
    const cameraTargetMeshOffsetPosition = cameraTargetMesh.getAbsolutePosition();
    camera.target.copyFrom(cameraTargetMeshOffsetPosition);

    // Player move
    ...
});

I think I’m misunderstanding how position updates are handled and would be thankful if anyone could shed some light on this. Thank you for your help! :smiley:

You need to:

  • set the camera target after you compute the new player position, as it depends on it
  • explicitely update the world matrix of the player / target mesh before getting the target mesh position. The world matrix computation is normally done by the system, but later in the process.

https://www.babylonjs-playground.com/#3EDS3A#78

4 Likes

Mindblown! Thank you so much, @Evgeni_Popov! :smiley_cat: