Reminder : You can find all the DarkRift2 related articles here
You can find the entire project on my official GitHub
Unity game architecture with DR2
There are a lots of way to design the game architecture in unity when working in a client/server environnement. It’s very important to think efficiently your architecture, otherwise you can be face of a wall at a certain point of achievement of your project.
In fact, when working with a DR2 unity embedded server, the common difficulty is to keep the scene equal between the server and the client.
For the tutorial, i will use the following implementation :
There are 3 blocks wich represents :
- Yellow : The server scene
- Blue : The client scene
- Green : All the game scene of our project
How they will work together ?
When we’ll launch our server, we’ll start the MasterServerScene (wich includes the DR2 server). This scene will load an additive specified scene that contains our game, and that’s all.
It will be exactly the same way of working
The first advantage of this architecture is that we are sure that the server and the client have the same game scene !
Create the main game scene
First of all, we need to create the main scene of our game. We’ll keep it simple by creating a scene with :
- A basic platform
- A bouncing ball (using the Unity 3D physics engine)
Let’s start by creating a simple scene named MainGameScene in the “Scenes” folder
As said, this scene will be shared by both server and client.
Create the platform
Create the platform by right clicking and add a 3D object > Cube
Name it “Platform” and fill the transform with the following values :
Position (0,-4,0) - Scale : (10,1,5)
Create the ball
First thing to make our ball bounce is to create a Physic Material that the “Sphere Collider” will use. So let’s create a new folder named “Physic Materials” and create a new Physic Material
Call it “BouncyBall” and configure it like that
Now, on the scene hierarchy, create a new game object called “BouncyBall”
Select the “BouncyBall” object in the scene, and add a Rigidbody component to this object
Finally, we just need to assign the material to the material property within the Sphere Collider component. Go to your Physic Materials folder, and drag the BouncyBall material into the material property of the component.
Ok, if you run the scene, you will see your ball bouncing, but, it’s a little dark, isn’t it ? Let’s polish a little the game. Add a directional light and configure it like that :
And the camera as the screen below :
The result must be something like that :
OK ! Well done, we now have the main scene game ! I totally agree with you, the scene has no interest but it’s an exemple to illustrate how the server will synchronize the bouncing ball with all the connected clients.
Yes, we will synchronize object based on physics ! That’s a good news that i havn’t shared you, isn’t it ? Our server will be authoritative.
Definition : An authoritative server doesn't let the client determines positions, action effect, ... That's the server role. Each synchronized objects are moved, affected, ... on the server. The server then sends data to clients.
On the next article, we’ll setup the ServerMasterScene with DR2.