4 player shared camera in BP

Attempting to get a single camera for a 4 player local co op game. My goals are to have the ability for players to come and go as they please. and the camera to change depending on how many players are in the game at any given time.

I currently have a camera system based off of this tutorial.

What I currently have:

  • 4 players default to use camera actor
  • clamps distance for 2 players
  • Moves based on location of two players

Current downfalls:

  • First and second player can drag each other around (not really a bad thing for my design)
  • When only one player the camera defaults to 0,0,0 and clamps the single player
  • any addition players above 2 are able to freely move off screen.

What I am looking for:

  • Have the camera check for current number of players
  • calculate the distance between them
  • if only one player then follow that player.
  • does not need to zoom in and out based on distance between players.

I can provide pictures of what I currently am using if needed.
I am thinking that a switch on int that runs off the tick that is based on the number of players in scene. 1 player will just follow that player. 2 players will be what I currently have. 3 players will calculate the center of a triangle. and 4 will calculate the center of a rectangle. Not sure if I am on the right track or just running the the opposite direction.

After some time. I found a solution.

Basically all I needed to do was grab all my players, and because I am only using one class I use a get all actors of class node. set the view with blend for how ever many it found, then a switch on int with -1 the length of players found.

depending on the number of players just add all the actor locations and divide by how many players are in the game. set the camera actors location to that result and it will always stay in the center of your number of players.

Something of note, Players can still leave the screen unless clamped or you adjust the spring arm length based on distance. Currently trying to figure out how to clamp more than two actors but my main issues has been answered.

would you be so kind as to provide a screenshot of that blueprint , i am dealing with the exact same issue and have a working model for 2 players based off of the same tutorial you read… in exchange i can help with any 4 player UMG questions you may have as I have just succesfully implemented drop in drop out player stats (that appear on screen as players enter game and disappear as players leave the game)

Hi,
Thank you for this great post. I’m also looking for a solution to implement 4 local coop. I need the camera to zoom in as they come closer and and zoom out when they move further apart from each others. Any help is much appreciated. Screen shots or links to videos is GREAT help.

Thanks in advance
R

Raed, I still have not correctly implemented the 4 player camera solution, I am using the 2 player camera solution provided by UE4’s 2 player camera documentation and also a rank system so the camera follows the 2 highest ranking players and the last 2 just have to follow them lol… What I do have functional is 4 player UI (with both drop-in drop-out and upon game start) implementation … A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums

few years ago someone posted this but until today I still don’t understand it lol give it a go and see what happens. If you get it to work please post a screen shot.

  - Imagine the locations of your four characters simply as points in space. As a fifth point you have the camera location.
    - Now find out which of the four points is closest to your camera point.
    - Now take the forward vector of your camera and find out at which point along the vector the distance to the point found in step two is shortest and remember that point. I will call that point 'p'
    - Now construct an imaginary plane that includes the point 'p' and has a normal vector that is parallel to the forward vector of the camera.
    - Now you need to compute the projection of all four points onto this plane.
    - Now you must determine the smallest rectangle that includes all four points on that plane.
    - This rectangle is the area of the level that must be shown on the screen. From this point on things should be simpler:
    - You need to compare the aspect ratio of your viewport to the ratio of the sides of the rectangle: If the aspect ratio of the rectangle is larger than the aspect ratio of the viewport, you will need the larger side of the rectangle for the next step. Otherwise you will need the shorter one.
    - From this point on you can use the script you allready have: instead of the distance vector between two actors you would use the vector determined in the previous step.

(ALL CREDITS TO Praseodyl)

hi again,
This is what I came up with. It’s not perfect but It’s an ok workaround. If you develop it further please share here.

Raed

Raed, that looks pretty good I am going to try it… I had used actor average location as well in another try but couldn’t figure out how to set the camera spring arm height with the 4 players…

I am going to play around with this , will definitely let you know if I “improve” on something…

Raed, the solution I came up with was to use the UE4 tutorial solution (from the link in tthe first post on this page and mix that with the super smash bros. camera tutorial https://forums.unrealengine.com/attachment.php?attachmentid=35405&d=1429292244

So pretty much at event begin play i get all actors of class for the player character (up to 4 players)… and then I use that as P1 P2 P3 P4 (if a reference is not valid I automatically make him to be p1)… from those 4 variables I then get the max of the float array, just like in that picture from the smash bros tutorial, but then i hook it up to a switch on int and if for example P1 & P3 are the furthest away (max of float array) then I set P1 & P3 to be what the UE4 tutorial was using as P1 & P2… but I renamed the variables so that they don’t clash… so I have P1,P2,P3,P4 & then CameraP1 & CameraP2 so that any 2 players can be camerap1 or camerap2

So basically I get whichever characters are furthest away and make them to be the P1 & P2 references that the UE4 tutorial uses… it has worked so far for me…

What type of game are you making?

Raed, the solution I came up with was to use the UE4 tutorial solution (from the link in tthe first post on this page and mix that with the super smash bros. camera tutorial https://forums.unrealengine.com/attachment.php?attachmentid=35405&d=1429292244

So pretty much at event begin play i get all actors of class for the player character (up to 4 players)… and then I use that as P1 P2 P3 P4 (if a reference is not valid I automatically make him to be p1)… from those 4 variables I then get the max of the float array, just like in that picture from the smash bros tutorial, but then i hook it up to a switch on int and if for example P1 & P3 are the furthest away (max of float array) then I set P1 & P3 to be what the UE4 tutorial was using as P1 & P2… but I renamed the variables so that they don’t clash… so I have P1,P2,P3,P4 & then CameraP1 & CameraP2 so that any 2 players can be camerap1 or camerap2

So basically I get whichever characters are furthest away and make them to be the P1 & P2 references that the UE4 tutorial uses… it has worked so far for me…

What type of game are you making?

dont know if someone still searching but nowadays for a 4 player camera or however many you want actually you can use the BP node called Get Actor Array Bounds which comes with a center vector that you can use as the center for your camera, it also has box extent Y & Z which you can use to calculate the size of the box and in conjunction with your aspect ratio preference you can use that to control any additional function such as locking players from moving out of a certain distance…

1 Like

![alt text][1]

This is my function for keeping 4 players in bounds of the screen (notice i also put a text render to show their X and Y positions for debugging)… this in conjunction with the get actors array

which i use to keep the camera centered with the 4 players (i actually use a tracker actor attached to them so that i can have the flexibility to attach it to another actor or pawn if required)… i had to offset my camera location by -200 because of the rotation of the view, i probably could have messed with the inRange as well to achieve the same results of keeping the players bound onto the screen, but this has been my method to implement. I have not found any other solution on the internet so far… so please share if you can make it better… [also notice i am using player controller 0’s viewport, i tried using each different controllers view for the function and they always returned NULL numbers so remember to use player 0’s view just like it is in the function, and then run that function 4 times (once for each players) if it seems like it isnt working, you might have the X & Y flipped like I did during the first few tests XD …

1 Like

Thanks this was the solution for me!

1 Like