2D Multi-Player Top-Down shooter .

Harshdhillon
4 min readAug 24, 2021

--

This project is alongside my journey of developing a basic version of 2d multiplayer Top down shooter Games using Photon 2(PUN).

Objectif of this project is to elucidate the basic understanding of Game design, Game manager , Networking & Server managing , Player Controller , Quick -match Room creation , Match Syncing and more.

Requirements:

  • Unity Engine(Unity Account)
  • Photon 2 (Account)
  • Visual Studio(With Unity plugin)
  • Basic Knowledge of C# & Unity .

The vague architecture plan of any Multi-player game :

Abstract cloud device(Server) create rooms .

Player(Client) join the room among other player.

Client and Server communicate with each other which turn into gameplay.

………………………………………………………………………………………

Further This basic plan can be split in two Game Network design

Client-Host server.

Client-Dedicated server.

_ _ _ _ _ _

Network of Device’s that exchange workload directly among other via host without any central server device using PUN(Photon Unity Network) .

Client-Host pros:

  • No need for a dedicated server, economical, scalable and easy startup.
  • Feasible for small groups.
  • Easy way to share data between players.
  • LAN hosting possible.
  • Host transferring(No server -down problems).

Client-Host cons:

  • The host computer will likely lag
  • Need to manage the loss of a peer (due to a network outage, rage quit etc.) or this could break the game flow.
  • Not much Hackproof as client can become host.
  • Difficult to achieve desired implementations.

Client-Dedicated server

Whole operation actually controlled & supervised by an dedicated authorized server .(Multiple servers can be used conditionally or for specific regions).

pros:

  • Need for a central server.
  • Not quite viable , Cost more.
  • Server maintenance , any problem could have serious gameplay drawbacks.

cons:

  • Long distance might cause server lag(Multiple server could be used , more cost).
  • Not much Hackproof as client can become host.
  • Difficult to achieve desired implementations.

For this tutorial we will be using Client-Host game design.

Wasting no more time… we’ll start

: Create Photon Account

:Create new application

: Note down your unique application id

: Download Photon 2 free Unity plugin.

: Import all files.

: When prompted for app setup enter the code of your unique photon application code

That’s all needed for the photon-unity setup.

………………………………………………………………………………………

Next Step

: Connecting to photon server via scripts.

Now we need the client device to connect to the photon server but we want this process to run in background, therefore we will create a lobby where player can wait until successful connection.

: First create &save a unity scene , named “Lobby_Scene”.

: We are not going deep in game so will not cover extra stuff, which a player can do while waiting rather just work on UI . Add the canvas from UI in scene hierarchy .

: Under canvas create a game object LobbyManager, which will controls all lobby activities. Create a script named PhotonLobby & assign it to the LobbyManager ,we will use PhotonNetwork which is photon class that handle’s PUN package.

: Create a static field to make sure no other instance of class is created and create Join and cancel buttons like in the code below.

First disable Join & cancel button as we want join to enable when we are connected to a server and cancel when we are in process of doing so.In Awake : we will initialize singleton ;In Start : we will to connect to the server by using connectUsingSetting()class which access data via PhotonServerSetting file in unity (as mentioned earlier )After connection photon will call OnConnectedToMaster() in which we will check if connection is successful or not .
If yes our battle/join button will come alive .
Now , our game will have quick match system.
To do so we will use
JoinRandomOrCreateRoom()(Player[client]will look for look for room if not found will create one)
.
Player could fail to join a room {reason could be room already full / no room exists) and the least we would like is to get notified for which we will use photon OnJoinRoomFailed.For joining server could handle it ,but for creating we can use Photon CreateRoom()method.This method require two parameter (String[room_name],roomOptions[room_properties].Using roomOptions we can make our room open for everyone in lobby, assign max numbers of players.Now room creation could also fail (reason could be room already exist ) approach for which could be OnCreateRoomFailed()method
where we could modify our strategy of creating room accordingly.
Last but not the least we assign our cancel button with photons LeaveRoom() method , it simply exit the room and return to lobby.

Result of all above should be similar to following demonstration

Next part : PlayRoom_Scene.

--

--