- Home /
Centering camera between 4 players
I'm trying to make a camera that will always target the "center" between 4 playes and zoom out if the distance between them increases (and zoom in when decreases).
I managed easily to get the center of those players, but I'm having a lot of problems with zoom in/out functionality expecially because I have to consider that a player me be also on a different height relative to the others.
Any help or links to tutorial is well appreciated
Edit 1 I'm asking also for a suggestion about how to calculate the center, my current idea is:
Find the point between point1 and point2 (let's call it between1)
Find the point between between1 and point3 (call it between2)
Find the point between between2 and point4
The other idea I have is this:
Find the point between point1 and point2 (between1)
Find the point between point3 and point4 (between2)
Find the point between between1 and between2
But with this, I'm having some hard times with 3 characters :\
Is this a side-scroller or arbitrary 3d movement? The problem is somewhat easier if the players are always in the same plane. Otherwise your camera needs to find a comfortable distance that can exist from many different vantage points, so you'd have to provide more constraints.
It's a 3D diablo-like game (I don't remember the name of that genrte). So people moves on the same plane, however they can be on different vertical axis, like if a stairs bring you a bit up, the camera should zoomout to let all characters fit the screen
Answer by Tasarran · Jan 30, 2012 at 10:37 PM
I would start the camera at the center you found, then zoom back a certain increment (you'll have to figure out what works for you)
Then check to see if the palyers are all visible, using Renderer.isVisible()
Then, if they are not visible, move back again and re-check.
You ought to be able to do this many times in a single Update, so you should only see the end result.
As a matter of fact, it would work better with a while() loop...
pseudocode!
while ( All four players !(Renderer.isVisible)) {
Zoom back;
}
I would suggest checking the renderer bounds, rather than isVisible. Wouldn't isVisible be true if any part, no matter how small, is visible? Or just the pivot point? The bounds will give you an idea of his total screen size, and you can add some padding for a border.
Good suggestion, can you also suggest me "how" to detect the center? I have some ideas but I'm not sure of it (see my edit).
That's technically a second question, you should really accept this one as answered and ask another, but I'll answer it here :)
The easiest way I can think of is simply to find the average of the positions.
Just add all the transform.position values for all the characters, and divide by the number of characters.
Should work for any number of characters...
Or you can find the median, as opposed to the mean (average). This means that you would check all the positions, find the $$anonymous$$/max in all three coordinates, thereby building a bounding cuboid. Then just find the center of that cuboid. You can add a margin to make sure that the outermost chars aren't right on the edge of the screen.
$$anonymous$$edian method is probably the best.
Well I wrote "centering" the camera and I hoped that the question was enough generic, but I don't have any problem in making a new question. The 2 methods you stated is those that I used 'till now when I tested them in game: the first one seems to not be the "center" when you look at it (the average). The median has the big problem of not considering the height (Y) of all players, so the camera doesn't zoom out enough most times. However, I have to test with your renderer.isvisibile method, I think it is ok if I use both of them.
You start zoomed in on the median, then zoom back until they are visible...
Shouldn't matter what axis they are more distant on, it should work...
Answer by Berenger · Jan 30, 2012 at 10:28 PM
Once you have the center, your zoom should be given by the player the farthest from it, and that distance. Then you just push or pull the camera along forward.
Read my edit, can you help with that if possible?
Your answer
Follow this Question
Related Questions
2 players 1 game field in shooter/card game 0 Answers
Multiplayer click and spawn 1 Answer
TPS camera in multiplayer... 2 Answers
Have Camera Render First Person Arms But Not Body. 2 Answers
How to make camera position relative to a specific target. 1 Answer