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.
» Running the example
In order to run the application follow these steps:
- make sure your SmartFoxServer 2X installation contains the BasicExamples Zone definition;
- copy the /deploy/extensions/fps folder to the server's /SFS2X/extensions/ folder;
- start SmartFoxServer 2X (v2.10 or later is highly recommended);
- enable the server's UDP listener if not already done;
- make sure the client runs on the same machine as the server (the IP address can otherwise be changed in the source code);
- open the /deploy/client/FPS.html file in a browser.
» Source code setup
The complete project is contained in a zipped folder. To access and build it please follow these steps:
- unzip the file contained in the /source folder;
- start Unity;
- in the Projects panel click on the Open other button and browse to the top folder of the unzipped package, then click Open;
- wait for the project setup completion (Unity needs to regenerate some libraries);
- go to the Project panel, click on the Assets/Scenes folder and double click on one of the scene files to open it.
All relevant assets are contained in the Assets folder; in particular the C# code is in the Scripts subfolder and the SmartFoxServer 2X client API DLLs are contained 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 a basic Extension document. Copy the content of the /source/server/src folder to your 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: