Multiplayer Shooter Game Beginnings

Multiplayer Shooter Game Beginnings

Unity setup with Photon Unity Networking (PUN)

·

2 min read

After creating multiplayer Pong, the next challenge that seems acheivable is to create a multiplayer shooter game.

For this, I opted to use Photon in Unity again. Here is the progress so far:

Game with two screens

The code can be found on Github here.


Step 1: Player Prefab and Movement Script

Firstly, I created a player prefab and associated it with a player movement script, cleverly named PlayerMovement. In this script, I set up essential variables for speed and velocity change. The script calculates the target velocity based on player input and current orientation. It's a straightforward setup, nothing too fancy. This prefab was then neatly packaged into the Resources folder.

Step 2: Networking with Photon

After learning a bit about Photon already I created a simple networking manager script. This script checks our connection status and ensures that we can join a room. Here's a snippet of the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;

public class NetworkManager : MonoBehaviourPunCallbacks
{
    public GameObject player;
    [Space]
    public Transform spawnPoint;
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Connecting to server...");
        PhotonNetwork.ConnectUsingSettings();
    }

    public override void OnConnectedToMaster()
    {
        base.OnConnectedToMaster();
        Debug.Log("Connected to server!");
        PhotonNetwork.JoinLobby();
    }

    public override void OnJoinedLobby()
    {
        base.OnJoinedLobby();
        PhotonNetwork.JoinOrCreateRoom("test", null, null);
        Debug.Log("Joined lobby!");
    }

    public override void OnJoinedRoom()
    {
        base.OnJoinedRoom();
        Debug.Log("Joined room!");
        GameObject _player = PhotonNetwork.Instantiate(player.name, spawnPoint.position, Quaternion.identity);
    }
}
💡
When the NetworkManager class inherits from MonoBehaviourPunCallbacks, it ensures that the necessary callbacks for Photon networking are inherited. This enables the class to override and define specific behaviors in response to events such as connecting to the master server, joining the lobby, or joining a room.

Step 3: Camera Controller

Within the Player GameObject there's the Main Camera itself and on the Camera a script named CameraController. This script just controls the camera movement so that it is in sync with the player movement.

Building and Playing

Clicking to play the game reveals the world after a brief rendering period.

The camera is positioned at the appropriate height on the player, but this might change as the player design is refined.

That's all for now! More details to come soon .....

Thanks for reading!

Molly

Did you find this article valuable?

Support Multiplayer Games Development by becoming a sponsor. Any amount is appreciated!