Unity multiplayer tutorial, physics calculations on server

Share

Creating and adding the Inputs components to enable player control

In this next step of this Unity multiplayer tutorial with physics calculations on server we will add the input management components to the RacketCreator and  RacketOwner prefabs. The Owner is the client side that should send the movement requests and the Creator is in the server side and should checks the request, calculates the interactions and send back the results to the client for displaying.

  1. In the Project panel, create a new folder named Scripts. In this new folder create a new C# script named InputReceiver and open it in the code editor.
  2. Paste in the InputReceiver script the following code to manage the inputs from the client:
    [snippet id=’114′]InputReceiver.cs – Unity multiplayer tutorial physics on server C# script code example download
    We defined the methods to be remotely called from the client, the effects will be applied to the object this script is attached.
  3. Add the InputReceiver script as a component to the RacketCreator prefab, because we should apply the methods defined in the script to the gameobject on the server.

But who is calling the RPCs that move the Racket? We need to write another scripts, client side, that reads player keyboard inputs and calls for the RPCs in the InputReceiver script.

  1. In the Project panel, in the folder Scripts create a new C# script named InputSender and open it in the code editor.
  2. Write the following code in the script to read player input and calls the RPC on the server :
    [snippet id=’115′]
    InputSender.cs – Unity multiplayer tutorial physics on server C# script code example download
  3. Add the InputSender.cs script as a component to the RacketOwner prefab, because it is the object in the client.

Our rackets, as in classic pong, will be able to move on only one axis, the vertical one. We successfully created the controlling backbone for the players, we need to prepare working on the server and client logic.

You can copy the pongserver scene on the pongclient scene because we need them identically from up to this point, from now on we will differentiate them to establish different behaviour between the client and the server.

Leave a Comment