Keep 2 players on screen by moving camera back
Hello !
I have a game where i have 2 players moving on a plane.
I want to keep them inside the camera view all the time so I need to back the camera when the move far away from each other.
Right now i'm doing it arbitrarily, by linking the camera distance with player distance proportionally but i'd like to find a way to do it "perfectly".
Here is a quick gif of how it looks like now :
If anyone has an idea.
Answer by Magius96 · Feb 27, 2016 at 06:39 AM
Let's think back to our lessons from Geometry and Trig, eh?
Ok, so let's visualize that we have two identical right triangles, TriangleA and TriangleB. The Hypotenuse of triangle a runs from player 1 to the camera, and the hypotenuse of triangleb runs from player 2 to the camera.
We also know from those lessons ages ago that if we count a single point on the triangle as our alpha point, that the triangle then has a opposite and an adjacent line. We can deduce that the length of the opposite line will match the distance from either player to the mid point between the players.
So far, we only know one angle, and one line, right? Wrong, we actually already know a second angle. The camera can only see to a specific angle of view. Let's say that the camera can see a full 90 degree angle. Then we know that the angle between the opposite line and the hypotenuse is half of 90 degreees.
Now, we have enough information to work with using what we know of SohCahToa.
Sine = Opposite / Hypotenuse Cosine = adjacent / hypotenuse Tangent = opposite / adjacent
What's that you say? Oh yeah, we only know one of those lengths. But we know two angles, using those two angles we can determine the third angle, because all the angles in a triangle add up to 180.
so we can use one of those angles to figure out the length of the adjacent line, which would be the distance the camera would need to be from the scene for both players to still be on camera.
adjacent = tangent * opposite
That's an inversion of the tangent formula above, we just multiplied both sides by opposite. so lets say that the players are 100 points away from each other, so the mid point is 50 points. And as we mentioned before our camera can see 90 degrees, so the angle of the hypotenuse from the opposite is about 45 degrees then our formula should look like this:
adjacent = Mathf.Tan(45) * 50;
Theoretically that adjacent value is the distance from the scene that the camera will need to be. However at this point I do need to point out that you will have to do some radian to degree transformations in all this mess. I don't remember off the top of my head at what point or in which direction it needs to be. I'm feeling like you should multiply the resulting adjacent value by Mathf.Rad2Deg to get the real results, but as I'm not actively working this out in Unity, I very well could be wrong.
While my answer is on the right track, I can guarantee that it's not spot on, but I hope this will provide you enough lead to find the correct result yourself.
Thanks for the answer. I didn't see it (it's been quite some time). I put the project on hold but I'll try to implement your solution as soon as I'm working on it again
Answer by jktaylo1 · Aug 20, 2017 at 09:52 AM
Magius is right with the trigonometry, but it's easier than this because tan(45) is 1, so just move the camera back away in the z direction in the value of half the distance between your players.
Your answer
![](https://koobas.hobune.stream/wayback/20220612065618im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Create Camera Bounds. In the camera with a different Viewport rect 0 Answers
Try To Set Camera Bounderis 3D 0 Answers
Camera bounds with zoomable camera 1 Answer
Clamping when Zoom changes? 0 Answers