- Home /
Creating a multiplayer game.
I have been using Unity for a while now, and I have been learning how to create a multiplayer game for the past couple of days. I have gotten as far as creating a server, multiple people joining it and each user sharing their object data (position, rotation) using NetworkView. The method I used was to use a player as a host of a server, and have him host it until he leaves the game. This method really makes no sense, since the player can leave at any time and the game will be over. I was wondering how could I create a different scene, or maybe even a different project to use it as a server and have it only share data. Like RPC commands, calculating if a player has been hit by a bullet etc.
Here's an example: Player 1 presses down the mouse button while aiming at another player. Player 1 then sends out an RPC call to the server telling it it's location and the point at which the player is looking at. Then the server decides if anyone has been hit, if so, take away health from Player 2.
Using this method, I would have to re-write every single function used by both Player 1 and 2, so I can use it in the server script. If that makes any sense.
Maybe i'm just being dumb and missing out on something really simple. I've seen really good multiplayer games created in unity. What is the most common way to share data between players and what approach should I take?
The more game logic the server itself runs, the less can be hacked or cheated. If the server controls who shot who and for how much damage, someone who cheated their client wouldn't be able to do anything with the modified client. Look into the problems with The Division, since $$anonymous$$assive decided to do most everything client-side. This opens up to letting people have infinite ammo, more damage than they should have, flying/teleporting, and etc.
$$anonymous$$ost games also make the server rollback time to the time that the player shot to line up the shots to see if it hit. If you don't do that, you have to lead your shots (watch or play any game from before the late '00s for examples really) while making the server rollback time allows each player to shoot at the other players directly. Lag is always a problem when playing online games, but you still want to make sure the game works as well as it can.
If one player were the server though, you'd have to deal with NAT. NAT makes it so people can't connect with each other, so you'd have instances of people not being able to play with others. Servers get around all that by just not having to deal with NAT, but since most everybody in their home is connected through a router they have to. If you had dedicated servers (or paid for the Unity's relay systems) you'd be able to connect damn near everybody on a pretty close to 100% of the time.
Plus, that player's computer would need to run the server as well as his game which could potentially make it so that some people couldn't play if they had weaker hardware. Games that are very small in player count (like 1v1 fighters) are better off if they connect straight to each other (less lag), but when you start getting into higher player counts you need servers to keep it as fair as possible for everybody.
Just look back into the original Gears of War's lag problem on 360 (and Gears 2 as well). You could easily tell who host was, and host always had the upper hand due to no lag.
Answer by Tsurugi21 · Jun 22, 2016 at 06:00 AM
Dedicated servers are quite simple really (at least the way I create them). They are just an instance of the game that doesnt spawn a player object (and thus dont play). Of course, if this is a matchmaking server, a masterserver would be a smarter choice. (please dont use unity's, it almost never works for me)
Your answer
Follow this Question
Related Questions
Very Confused on Networking HLAPI Practices 1 Answer
Do i need to have 2 seperate apps communicating for server/client relationship? 2 Answers
"Trying to send command for object without authority" warning but the object is the localPlayer 1 Answer
All players number in Network 1 Answer
Question regarding Server-Client approach in Unity 0 Answers