Contents - Quick Links
- 1Unity multiplayer tutorial, physics calculations on server
- 2Creating the pong field
- 3Creator and Proxy prefab and adding components
- 4Creating and adding the Inputs components to enable player control
- 5Creating the Server and the Client connection logic
- 6Implementing the ball
- 7Building the game .exe and Unity full project download
Creating the pong field
As we created a new project we need to create the graphics assets that will be rendered by the engine, we will need a pong field where we can play, the racket that each player will control and the ball that will be controlled by the physics on the server reacting to players interaction.
We will need also to create two different scene in our project, one named pongclient and the second named pongserver, since we are in a multiplayer environment client and server will do different things and must be differentiated. Follow the next steps of this unity multiplayer tutorial:
- With the new scene still empty (just the Main camera should be present), Save Scene As two times, the first time with the name pongclient and the second time with the name pongserver.
- Open the pongserver scene as we will continue working on that first.
- In the uLink settings, we must set the server in authoritative mode : in the top menu click on uLink->Edit Settings and check the Authoritative Server checkbox.
- Create the playing field, we will need a floor, 2 walls and some fancy lights. You can use simple primitives and scale them to get the results, make sure to pack all of the field’s objects into one parent object that we will call PlayingField. You can achieve this by creating an empty game object and then drag and drop every single game object that are part of the field into the newly created empty game object.
- Make sure to reset the PlayingField gameobject position to 0,0,0 as you can see in the previous picture.
- As we will need the same level configuration between server scene and client scene, you can now save the pongserver scene as pongclient, then reopen the pongserver scene to continue working.
Creating the rackets for the players
Next focus of this unity multiplayer tutorial are the pong rackets. The rackets will be controlled by the player, but since we are in an authoritative server setup the player inputs must be validated by the server so the player will send a call to the server saying that the player wants to move the racket and the server will deal with the effects and send back to the client the updated position.
We will need then to create three prefabs that will be instantiated by the server at runtime when a player connects, these prefab will have different roles :
Owner is the prefab instantiated on the client for the player’s racket. Motion of this object is controlled by the player.
Creator is the prefab instantiated on the server for every player connecting to the game. It has fewer scripts than Owner because we don’t need fancy effects on the server, we could make the server even without the camera.
Proxy prefab is instantiated in clients for the opponent player racket.
- In the Hierarchy panel create a Cube, and give it a rectangular shape, as one of pong’s racket.
- in the Project panel create an empty prefab in the Resources folder (very important as prefab needs to be in that directory to be called by name by uLink methods) and name it RacketOwner. Drag and Drop the Cube in the Hierarchy inside the empty prefab RacketOwner
- Delete the Cube you modified in a rectangle from the scene Hierarchy panel as we can now use the prefab.
We have our basic scene environment ready, in the next step of this Unity multiplayer tutorial with uLink and physics on server we will be talking about server-client roles!