# How to center a camera between two player sprites?

So that when a player moves, the other player isn’t cut out of the screen?

Similar to Street Fighter fight cam, where it has limits on narrowness and wideness, but so that the players can move around without the position of the other causing problems…

To center the X position of the camera, you can use the “Camera center X position” action.
To center it between two objects, you can use something similar to this:

This will set the camera position to sum of position of the first object and half of distance between the two objects.

You can also use Player2.X() - abs((Player1.X() - Player2.X())/2) to achieve similar results.

If this causes both your objects to move out of the camera view, you can set a condition that checks the position of one of the objects. For example, check when the X position of Player1 is greater than Player2.X() - 500.

3 Likes

Cool. I used your code, and it does in fact place the camera center between the two objects. Both objects are therefore located outside of the viewscreen at the same time.

The second piece of code affects things. It makes it so the centered camera is centered to a certain point of movement away from the center (by either player). After that point, either player can exit the screen be continuing away from the otherplayer, while the otherplayer remains in the screen.

Is there a way to make the camera zoom, though, so that when the characters get further away, the camera just pans out and they don’t go out of the screen.

Second part: is there a way to after that, make it so the zooming out stops at a specified point (so we don’t get toooo panned out), and at that point the characters are blocked from exiting the screen?

Although I’ve never tried it, I think this should help.

• Decide the distance for which the original zoom is to be kept. Let’s assume it to be 300. Also let the zoom stop distance be 600.
• Use the condition to check when the X position of Player1 is less than abs(Player2.X() - 300) and add another condition to check if the X position of Player1 is greater than abs(Player2.X() - 600).
• For these conditions, set the action to Change camera zoom and set the value equal to 300/(abs(Player1.X() - Player2.X())).
This should solve the zoom out problem.
• For stopping the zoom at a point, create another event and set the condition to check when the X position of Player1 is less than abs(Player2.X() - 600).
• Set the condition to change camera zoom to 300/600. This will set a fixed value for the zoom. To stop the objects, you can add a force to them in the opposite sides.

This is pretty good.

It gets close, actually.

Where the camera causes problems for the user is sometimes, when one player moves away, it just follows that one player, and the other then goes out of the screen. When he goes close again, the camera snaps back to the center position between the two players. There is also a bit of a jolt when it transitions from (initial) camera one to camera two (further away), but after that the transition is smooth when going between one and two and back again (not sure why this is). Also, the camera has the problem of cutting out the y axis when the players step onto new platform heights.

Is there another way to do this, where the first thing would be:

1.center the camera between the two (so it’s always centered)

1. when less/equal to 600, change zoom to 500

2. when more/equal to 601, change zoom to (like it is in your code)

Maybe it could work like,

1. place an (invisible/noninteractive) object equidistant between PlayerOne and PlayerTwo.

2. set camera zoom to focus on NewObject at distance of two characters + 100 on each side

3. minimum camera size 500

4. maximum camera size 1000

I’m not sure if those things are possible, though.

Hi

I use 3 object to center camera. Here Spider Killer by NoblehillGames example when use two player mode. Hope help too.

Nice. I also am using Object3 as the camera now (in another question post on this board).

But the next step didn’t get answered yet anywhere. I wonder how to then zoom the camera so it includes the two players whether they’re close or far (still centered on the object between them), and up to a certain point of far or close (so we don’t get too zoomed out)

1 Like

Im not sure what you think. My bad English. This is Finnish, but you get the point.

Thanks for trying.

I tried figuring this out, but I’m not familiar enough with Gdevelop commands and couldn’t find the right ones to match yours.

But I translated the words. Maybe someone else can explain what commands are used (in English)?..

kentan alkaesa
tee
etaisyys
een on alle
pikselia
muuttuja
siirra
sijaintiin
voimalla

field begins
tea
distance
een is less than
pixels
variable
Remove to
location
by force of

Hi!
Sry that Finnish language. I find this. Hope working with you.

And there you find other examples too.