Back To Home

Photon

5-04-2024


Taming the Multiplayer Beast: Understanding Photon and Networking Basics

Welcome to our Photon Unity tutorial series! In this series, we'll explore game development using Photon Unity Networking (PUN). This series is here to guide you through the process in a clear and easy-to-follow manner.

Throughout this series, we'll cover everything you need to know to get started with Photon Unity Networking. We'll learn how to set up a multiplayer environment, synchronize game objects across the network, handle player connections, and much more.


1. Understanding Photon and Networking Basics :

  1. Photon is a tool that helps games connect players together over the internet.

  2. It's like a bridge that lets players from different places play together in the same game.

  3. With Photon, you can make multiplayer games where players can interact with each other in real-time.

2. Setting Up Your Project:

  1. First, you need to have Unity installed on your computer.

  2. Then, you'll add Photon to your Unity project. You can do this by downloading and importing the Photon Unity Networking (PUN) package from the Unity Asset Store.

  3. Once imported, you'll see Photon's scripts and folders in your Unity project.

3. Creating a Photon Account:

  1. Before you can use Photon, you need to sign up for a Photon account. It's free to sign up.

  2. After signing up, you'll get an "App ID" from Photon. This ID helps Photon recognize your game when it's running.

4. Setting Up Your Game in Photon:

  1. In your Photon account, you'll create a new "application" for your game.

  2. Here, you'll use the App ID you got earlier to connect your game to Photon.

5. Understanding Photon's Concepts:

  1. Photon uses the concept of "rooms" for multiplayer games. Think of rooms as different spaces where players can play together.

  2. Players can join existing rooms or create new ones.

  3. Each room can have its own rules and settings.

6. Creating Multiplayer Gameplay:

  1. To create multiplayer gameplay, you'll need to synchronize actions between players.

  2. Photon provides methods for sending and receiving messages between players. For example, when a player moves their character, that movement needs to be sent to all other players in the same room.

7. Testing Your Game:

  1. Before releasing your game, it's essential to test it thoroughly.

  2. You can test your game locally in the Unity Editor by running multiple instances of your game or by creating an android build.

8. Building and Publishing Your Game:

  1. Once you're satisfied with your game, you can build it for different platforms, such as PC, mobile, or consoles.

  2. When building your game, make sure to include Photon's settings and configurations.

  3. Finally, you can publish your game for others to play and enjoy!


Let's see how Photon works behind the scene :

  1. Networking :

    Imagine you're playing a game with your friend online. You move your character, shoot, or do whatever in your game, right? Now, Photon is like a messenger that quickly tells your friend's game what you're doing and vice versa. So, when you move your character, Photon sends a message to your friend's game saying, "Hey, they moved their character to this spot!"

  2. Rooms as Meeting Places :

    In a multiplayer game, players usually gather in virtual "rooms" where the action happens. Photon manages these rooms. It's like booking a virtual conference room where you and your friends can hang out and play together.

  3. Smooth Gameplay Sync :

    Ever noticed how, in a multiplayer game, it feels like everyone is playing together in real-time, even though you might be miles apart? Photon makes that happen by syncing the game's action across all players' screens. So, when you shoot a monster in your game, everyone else sees it happen at the same time.

  4. Fast and Reliable :

    EPhoton's servers are like super-fast messengers. They make sure your messages get to your friends' games lightning-fast and without getting lost along the way. This makes for smooth and reliable multiplayer experiences.

  5. Customizable :

    With Photon, game developers can tweak and customize how their multiplayer game behaves. Want to add voice chat? Photon can handle that. Need to create custom game rooms with special rules? Photon's got your back.

Client-Side Logic and Server Communication in PUN :

    In Photon Unity Networking (PUN), the server acts primarily as a messenger, relaying messages between clients. PUN does not provide server-side logic execution, and developers must handle game logic on the client-side. Here's a bit more detail on how this works:

    Client-Side Logic Execution :

  1. In PUN, each client runs its own instance of the game logic. This means that player actions, such as movement or interactions, are typically processed directly on the client where the action originates.

  2. When a player performs an action, such as moving their character, that action is executed immediately on their local client. The client then sends a message to the server (and to other clients in the same room) informing them of the action.

  3. Message Relaying :

  4. The Photon server acts as a relay, forwarding messages between clients in the same room.

  5. When a client sends a message to the server (e.g., "Player A moved to position X"), the server broadcasts that message to all other clients in the room, so they can update their own game states accordingly.

  6. Lack of Server-Side Validation :

  7. Since the server doesn't execute game logic, it doesn't perform validation checks on player actions.

  8. This means that, by default, clients are responsible for validating their own actions. For example, if a player tries to move their character through a wall, the server won't prevent it; it's up to the client to check for such invalid moves and handle them appropriately.

  9. Cheating Prevention :

  10. Because client-side logic can't be fully trusted, developers need to implement additional measures to prevent cheating.

  11. Common strategies include server-side checks for suspicious behavior, anti-cheat software, and encryption to protect sensitive data.


In summary, PUN focuses on managing network communication and synchronization, while developers are responsible for implementing game logic and ensuring the integrity of player actions on the client side. It's essential to design your game with this client-server architecture in mind and implement measures to maintain fairness and consistency across all players.

Please read => Part 2 for further exploration of Photon Unity Networking features and implementation details.


I hope you found this blog post enjoyable!


More Blogs

See All