- Home /
Two or more players in tank using mirror
I'm using Mirror library to make a multiplayer game and the connections are all local for now. I'm using a server/client and client for now and they can now both connect. I'm starting with only a two player multiplayer game and my next phase I will probably be adding a 3rd player. These are the mechanics I was planning for the game:
One tank and two connected players and probably a 3rd player(Commander) using mirror library.
The tank will consist of a driver Player 1 where he can also control the canon.
Player 2 will only be controlling the .50 caliber machine gun.
Possibility of adding a commander(3rd player) that can view from top with an airplane and can drop bombs.
What is the best way to approach the following:
How should I break the Tank gameobject to get it prepared for a multiplayer game?
Where in the tank should I spawn the player driver and canon controller?
Where should I spawn the player .50 caliber gunner?
Who should be the server/client and client or does that matter?
When moving the tank how should the player one and player two be handled across the network if one of those players have to be a server/client and the other client?
The game that I can think of is Battlefield 4 where player one can drive the tank and move the canon to shoot, player two if it gets inside the tank it can control the .50 caliber gun. Anyone that knows how this is handle or the best way that I can accomplish this will be greatly appreciated. Trying to get some ideas of the best way to handle multiplayer players entering cars, tanks, helicopters where player one and player two can still do some functionality with the controller.
Most games like Battlefield 4 allow clients to own themselves and their data. Not sure exactly the specifics of the design, but the server will generally own shared things like building state (destructable walls etc) and when clients shoot something they let the server know. This lets the client perform really well and be very responsive but opens it up to easy cheating. I expanded on my answer a little below. In order to suggest what exactly to do with Mirror in your game, you need to decide on your client/server design first. It sounds like a client-auth where the clients own their player is the best design for your game, however the shared Tank will probably need to be Server owned.
Answer by GetLitGames · May 07, 2021 at 12:18 AM
If you don't need to do it, I wouldn't spawn a body for them - just let them control a camera and you will use their input to control things on the tank. Player 1 would control turret and Player 2 would control the gun. You don't even need to spawn them in any certain place for this, you just need their input data and some little system of who is controlling which piece so you know where to allow that input to be used. The local camera view is irrelevant for multiplayer and you don't need to do anything in networking for that. You mostly want the input to control the tank. The tank's movement and rotation needs to be replicated, so those can be two separate things and all you focus on is how the input from the two players changes the movement/rotation of the piece they control. So to summarize, you setup that the transforms are replicating their position/rotation. Secondly, you need to have the input from the local player affect the movement of the tank and some way of knowing which player controls which piece. If you are already replicating the tank, you don't need to do anything about communicating input from one player to another because the pieces will always replicate their position/rotation - you just need to affect the piece locally. The data you need to share via Mirror will be who controls which piece, so that the input on the remote player only affects what he controls and the input from the local player affects theirs. This could simply be a number somewhere that says Turret:1 and Gunner:2 so you can have code that sees that Player 2 is on Gunner etc. Later you will probably want to think about how to secure that data so that a cheater can't just change it locally and it changes on everyones machine. Usually there is a concept of Ownership so only the Server/Owner can make the change, so if you allow clients to host the game they would probably be considered the Server and be the owner. If you want to do dedicated server only then you would probably have the server own the tank and control the data, and clients would just send input to the server and the server would move the tank. During startup/game setup the Server would somehow give players a choice of which piece to control and set the data that determines that, and clients can never change it without asking the server to do it. So it depends on what you decide to go with for the design of your game too. So when inside the Tank, they have no visible body and you just deal with placing the local Camera in position for the view you want and send their local input to the server to affect the tank. Once they exit the vehicle you enable the body and switch cameras and the input affects their body. You could have three separate pairs of camera + scripts for input and only enable one pair of Camera+component. Probably setting up the tank with two Cameras on it and knowing which player has which position and leaving one disabled (the one the local player is not using) and disabling the FPS body camera along with it's input and enabling the right camera/input component pair. So most likely an FPS body with it's Camera + input component/script, and a tank with driver Camera + input component and gunner position with Camera + input component. Once you switch from FPS body to tank, you disable the camera and input component on body and enable the designated pair of Camera+input, either driver or gunner. This way your code is clean and only deals with input in the way it needs to, whether its controlling the body or driving the tank or the gunner position to affect the Camera view and the piece controlled.
As I'm working on this I will probably have more questions for you in the future. @GetLitGames thank you for taking the time to answer.