0
\$\begingroup\$

I've been dabbling in Unity. I learned the basics and I've created a few small games and one larger one. All of them were single player games. Now I want to create a game that's a 2 player - pretty much "Catch" with extra steps (two players, each competing to collect as many collectibles as possible while trying to get the other to drop their collectibles.)

I've been trying to understand what the best tool would be to handle the networking and such but all the tutorials speak of creating multiplayer games. I want only 2 players, and no tutorial I've seen explains HOW exactly one player can create a host and invite a certain one other player to the session. It's shown nowhere I've seen, and I'm getting frustrated by the multitude of "create a multiplayer in 5 minutes" tutorials that show how to add a network component but say nothing of how it's supposed to work over multiple computers.

I just need an explanation of how it all works. Thank you.

\$\endgroup\$
2
  • \$\begingroup\$ Do you want to do this over a LAN (e.g. both players on the same wired / wireless network) or over the Internet? For the latter case, are you using a service that already associates players with account IDs or a friend list for invitation purposes (e.g. Steam, Google Play Games...), or are you needing to invent that tech from scratch too? Is this for PCs, mac, phones, tablets...? \$\endgroup\$
    – DMGregory
    Feb 6 at 19:30
  • \$\begingroup\$ Not an answer since it is more a recommendation (which are off topic), I have personal good experience with Mirror as networking. They have a good documentation and a few example games that allow both for direct hosting the game and a second Player joining or setting it up as a server. It takes more effort than just slapping a network component on it but is not too much. If you have a specific question to a choosen one, you might get more answers for that (Pun, Mirror, Photon) \$\endgroup\$
    – Zibelas
    Feb 6 at 20:57

1 Answer 1

1
\$\begingroup\$

Hey Kaiannae if you're considering venturing into multiplayer game development, a convenient approach is to utilize established network frameworks such as Mirror or Unity's Netcode for GameObjects.

In essence, these frameworks offer a tool known as the NetworkManager. This versatile component efficiently manages the transmission of data. Simply attach the NetworkManager to an object within your project, and you gain access to its streamlined methods. This allows you to effortlessly either host your own game or seamlessly join an existing one. It's a refined solution that streamlines the complexities of multiplayer networking.

// example using mirror
// this is everything you need in terms of hosting and connection
NetworkManager.singleton.StartHost();
NetworkManager.singleton.networkAddress = IpInput.text;
NetworkManager.singleton.StartClient();

Note: From here on Mirror is the example framework

When it comes to moving and animating characters in Mirror, you've got handy tools like NetworkTransform and NetworkAnimator. These let clients take control of their own character's movement and animations without waiting for the server's approval. So, you can see your character walking and moving around the game world right away.

But, when you want to add features like picking up coins, things get a bit trickier. You'll need to understand how information is sent between the server and clients.

In our example, imagine you're both running the server and playing the game. This gives you a good sense of how information flows back and forth between your game and the server.

// Use a ClientRpc to synchronize coin pickup across all clients and the server
[ClientRpc]
void RpcPickupCoin()
{
    // Destroy the coin GameObject for all clients
    Destroy(gameObject);
}

// Use a command to execute on the server to destroy the coin
[Command]
void CmdPickupCoin()
{
    // Execute the RPC to destroy the coin for all clients
    RpcPickupCoin();
}

// Detect when a player interacts with the coin
private void OnTriggerEnter(Collider other)
{
    // Check if the object colliding with the coin is the player
    if (other.CompareTag("Player"))
    {
        // Only allow the local player to send the RPC
        if (isServer)
        {
            // Execute RPC to destroy the coin for all clients
            RpcPickupCoin();
        }
        else if (isOwned)
        {
            // Call the RPC on the server to destroy the coin
            CmdPickupCoin();
        }
    }
}

I hope you find this helpful, but remember, there's a lot more to learn about making multiplayer games. It's a good idea to dig deeper into the topic before you start creating your own.

Greeting :)

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I see, thank you!. I wonder what would happen though, if two players touch a coin at the same time. This goes into the realms of shared memory, I think. My question is, is the network manager something that would work for connection over the internet, or only locally or via LAN? \$\endgroup\$
    – Kaiannae
    Feb 7 at 21:02
  • \$\begingroup\$ Hey there! :) To prevent duplicate pickups, the server manages coin collection. Here's the process: - If you're the server, you can remove the coin. - If you're not, just ask the server to do it for you. The network manager handles both LAN and internet connections. Simply open a designated port for data transmission. I forgot to tell you about the transport component for the network manager. Add this component to the same game object and assign it in the network manager component, where you define the port then. I hope this is helpful - greetings :) \$\endgroup\$ Feb 8 at 8:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .