FollowCamera: Look at point other than lockedTarget's origin

I’m using a FollowCamera with lockedTarget set to the player’s mesh. But the mesh’s origin is at his feet, so when the camera zooms in, it’s looking at the floor… I would prefer it look over the shoulder. :slight_smile: The camera’s heightOffset property changes the camera’s position, but not where it’s looking. We need something like a “targetOffset” property, so I tried adding one:

followCamera.js (v5.7.0):

 86:    _this.lockedTargetOffset = Vector3.Zero();
...
 93:    FollowCamera.prototype._follow = function (cameraTarget) {
...
101:       var targetPosition = cameraTarget.getAbsolutePosition();
...
120:       this.setTarget(targetPosition.add(this.lockedTargetOffset));

Seems logical enough, but it has no effect. I can see the code changes in the Chrome inspector, and setTarget does receive the updated vector, but the camera’s rotation doesn’t change. Can someone please point me in the right direction? I’d be happy to submit a PR. Thanks.

cc @PolygonalSun

Well, the first thing that stands out is that you’re adding your offset to the absolute value of the targetPosition. While the y value would be relatively fine, your x and z values would move your camera in world space so your camera wouldn’t maintain the correct offset location if the camera rotates.

Overall, I’d recommend taking a look at how the ArcRotateCamera implements targetScreenOffset as that might provide a bit of inspiration. If you do get it working and submit a PR, feel free to tag me as a reviewer.

1 Like

Could also try creating a child with the required offset:

const cameraTarget = new BABYLON.AbstractMesh("cameraTarget", scene);
cameraTarget.parent = playerMesh;
cameraTarget.position.y = 2;
camera.lockedTarget = cameraTarget;

Interesting thread for us @waverider404

1 Like

This is a good idea!