SFS2X Docs / ExamplesUnity / fps
» First Person Shooter
This examples has taken the Unity "3rd Person Shooter" demo as basis to create a full blown server authoritative first person shooter using SmartFoxServer 2X. Additionally it utilizes SmartFoxServer 2X's ability to mix TCP and UDP based messaging.
TCP is the most common protocol deployed on the Internet. It’s dominance is explained by the fact that TCP
performs error correction. When TCP is used, there is an assurance of delivery. This is accomplished through a number of characteristics including ordered data transfer, retransmission, flow control and congestion control. During the delivery process, packet data may collide and be lost, however TCP ensures that all packet data is received by re-sending requests until the complete package is successfully delivered.
UDP as well, is commonly found on the Internet; however UDP is not used to deliver critical information, as it forgoes the data checking and flow control found in TCP. For this reason, UDP is significantly faster and more efficient, although, it cannot be relied on to reach it’s destination.
All critical game informations between the server and client are sent via TCP for reliable delivery. This could be shooting for example, or animation sync, damage messages, etc. On the other hand, sending updates to the players Transform — which is continuously sent to all clients - is utilizing UDP.
The game features a simple lobby and multiple games which can run at the same time as individual arenas.
» Setup & run
In order to setup and run the example, follow these steps:
- make sure your SmartFoxServer 2X installation contains the BasicExamples Zone definition;
- copy the /server/Extension_Java/deploy/fps folder to the server's /SFS2X/extensions folder;
- start SmartFoxServer 2X (v2.13 or later is highly recommended);
- enable the server's UDP listener using the AdminTool's Server Configurator module, if not already enabled (requires a restart);
- start Unity (v5.6 or later required);
- in the Projects panel click on the Open icon and browse to the /client/FirstPersonShooter folder, then click the Open button;
- wait for the project setup completion (Unity needs to regenerate some libraries);
- go to the Project panel, click on the Scenes folder and double click on one of the scene files to open it (i.e. Login scene);
- if SmartFoxServer and Unity are not running on the same machine, change the IP address of the server in the inspector of the Controller game object in the Login scene; also make sure the UDP port setting matches the one set in step 4;
- click on the Play button to run the example in the Unity Editor, or go to the Build settings panel and build it for your target platform of choice.
All relevant client assets are contained in the root folder of the project; in particular the C# scripts implementing the login and lobby logic are in the Lobby/Scripts subfolder, while the actual game logic is contained in the scripts under the Game/Scripts subfolder.
The SmartFoxServer 2X client API DLLs are in the Plugins subfolder: read the introduction to understand why multiple DLLs are used.
The game features a server-side Extension, as described in the overview. In order to access its code, create and setup a new project in your Java IDE of choice as described in the Writing the first Java Extension document of the Java Extensions Development section. Copy the content of the /server/Extension_Java/source/src folder to your Java project's source folder.
The First Person Shooter was developed by Full Control. You can download and read the developer's walk-through, which discusses the main SmartFoxServer 2X features implemented in the game and the techniques used to handle realtime user interaction.
Please note that the walk-through document linked above refers to a slightly different version of the game, still featuring the Unity's legacy GUI system for the Login and Lobby scenes. For this reason some of the screenshots in the document will differ from what you see when loading the project in the Unity Editor. In fact the example has been updated to support the new UI system introduced in Unity 4.6. Please refer to the Tris game tutorial for a description of the Login and Lobby scenes.
» More resources
You can learn more about the involved SFS2X features by consulting the following resources: