Stop FollowCamera from rotating with the target mesh

Hi everyone! Quick problem here that needs some clarification :slight_smile:

I have a mesh that I’m following with a FollowCamera. Trying to implement pathfinding (still something wrong with it but that’s not the point) and when the mesh rotates, my camera that is following the mesh rotates with it. Like so:

Is there any way to prevent this rotation easily? I want the camera to be fixed to a certain angle at all times…

Hey @Panuchka. Pretty new here so take my advise with grain of salt but I would like to suggest alternative approach.

For example, have you looked at ArcRotateCamera - it gives you manual control over rotation around focus point. Might be even more fun for your use case. :smiley:

You could just set it exactly how you want it and then in your update step use move the camera along with your target updating position with movement delta and using camera.setTarget(mesh) (docs) to refocus it on the target.

Check this out. ArcRotate (without attaching controls) instead of follow that does what you need I believe.
https://www.babylonjs-playground.com/#4TS4WS

1 Like

@Panuchka you got me interested in how follow camera actually works. Tested it in the playground.
https://www.babylonjs-playground.com/#SM9IPQ#1

Camera stays at the constant distance the target, “following”. It seems it does follow the target exactly as expected. :thinking:

Thanks for reply @MarkBevels , ArcRotateCamera seems better suited in my case :sunglasses:

Managed to twist my brain into correct way and added:

targetCamera.target = playerMesh.position
targetCamera.position = playerMesh.position

Which of course work since the position is the center which the camera rotates and target keeps the camera pointed around the player.

Glad you found it helpful but…

targetCamera.position = playerMesh.position

? :thinking:

I don’t fully understand why you would have set camera position to player position? I can’t see how that would work without additional position vector. If camera is in the same position as your player mesh I don’t think it would render what you expect.

Shouldn’t that look more like…

targetCamera.target = playerMesh.position
targetCamera.position = playerMesh.position + cameraPosition

where cameraPosition would be a constant distance vector between camera and the player. :slight_smile:

Yeah you are right! I was testing on my lunch break from real work so I was in a hurry. The position should be offset a bit, I did it like this now and it seems okeyish:

targetCamera.setPosition(new Vector3(playerMesh.position.x + 10, targetCamera.position.y, playerMesh.position.z))

Yup! Makes sense. :smiley: