SFS2X Docs / ExamplesUnity / first-person-shooter
» First Person Shooter
The First Person Shooter example and the walkthrough document below have been created by third-party developer Full Control a few years ago. Game client's graphics and some Unity development practices aren't up to modern standards anymore, but this is still a great example of how to write multiplayer code and handle interaction with SmartFoxServer and remote clients in a realtime scenario.
Also, screenshots and features described in the first chapter of the document will differ from the current state of the project, as the example's Login and Lobby scenes have been updated to match those from other examples. Please refer to the Lobby: Basics tutorial for more information.
This examples has taken Unity's "3rd Person Shooter" demo as a 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. Its 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 its 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 executed using 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:
- unzip the examples package;
- make sure your SmartFoxServer 2X installation contains the BasicExamples Zone definition;
- under the SmartFoxServer's /SFS2X/extensions folder, create the FirstPersonShooter subfolder;
- copy the /Server/FirstPersonShooter/Extension-Java/deploy folder's content to the server's /SFS2X/extensions/FirstPersonShooter folder just created;
- start SmartFoxServer 2X (v2.13 or later is highly recommended);
- if not already active, enable the server's UDP listener in the AdminTool's Server Configurator module (requires a restart);
- launch the Unity Hub, click on the Open button and navigate to the /Client/FirstPersonShooter folder;
- if prompted, select the Unity Editor version to use (v2021.3 or later is recommended);
- go to the Project panel, click on the /Assets/Scenes folder and double click on the Login scene to open it;
- 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.
The C# scripts implementing the login and lobby logic are contained in the /Assets/Scripts folder, while the actual game logic can be found in the scripts under the /Assets/Game/Scripts folder. The SmartFoxServer 2X client API DLL is in the /Assets/Plugins folder.
» Server-side Extension
The example features a server-side Java Extension. 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/FirstPersonShooter/Extension-Java/src folder to your Java project' source folder.
Download and read the developer's walkthrough, which discusses the main SmartFoxServer 2X features implemented by the game and the techniques used to handle realtime user interaction.
You can then proceed to the next example in this Unity series to learn new features of SmartFoxServer.
» More resources
You can learn more about the SmartFoxServer concepts discussed in this example by consulting the following resources: