# Follow camera target between two players - new BJS user

Hi guys, I am very new to Babylon but Im really liking it so far.
I have been trying to make a camera follow a target between two players (on the x axis).
To show both players on the screen at all times. Think Street fighter etc.

Can anyone help me out as i am also very new to any kind of programming.
Thanks!!!

Welcome ~ Thatâ€™s obviously not good enough, and Iâ€™m interested in a good answer.

1 Like

Hello and welcome to the Babylon community!

I think @muskâ€™s solution is a great starting point It can be refined and customized a bit by calculating a â€śgeneralâ€ť case, in which, given the two playersâ€™ positions, and a desired â€śmarginâ€ť to the borders of the screen, you calculate the desired cameraâ€™s parameters - camera position, target, field of view (FOV), etc - to result in the proper viewing frustum:

The solution to this will involve, fortunately or unfortunately, maths I recommend taking a look at Scratchapixelâ€™s camera lessons as they give a nice overview of how a 3D camera works: The Perspective and Orthographic Projection Matrix (Building a Basic Perspective Projection Matrix) (scratchapixel.com). Since Babylon.js already calculates the projection matrix for you, you just need to think about how the parameters will influence on the frustum, and plug them in Babylon.

2 Likes

Thanks Musk!

Thanks for getting back so quickly! Iâ€™ll look into it and give it a go.
This approach sounds similar to this unity tutorial video? Camera - tracking two objects at once --- Unity Tutorial: short and quick - 125 seconds - YouTube

I also came across this unity video MULTIPLE TARGET CAMERA in Unity - YouTube
which uses bounds to centre the camera.

Maybe this could be done in Babylon also?

Thatâ€™s pretty much on spot! You can use the cameraâ€™s transformation matrix ( Camera | Babylon.js Documentation (babylonjs.com)) to get from world to viewport coordinates, and the bounds approach is also a pretty good one (I like it even more than the first one to be honest)

1 Like