_ ✧ *:・゚✧ _ online multiplayer how-to _ ✧ *:・゚✧ _

this is a very simple guide on how to set up an online multiplayer game using unity. this is more for myself so if i ever mess up my game project (which happens a lot lol), i can use this as a guide!

this kind of set up is very basic, upon loading the game you will automatically connect to a room (there is no lobby, no start button, you just get started right away). there is little gameplay actually involved, since this project is for my virtual gallery (see the demo in action here). this assumes you have a basic understanding of unity. scripts will be provided as well!

note: check back for updates - eventually i will add how to sync player animations, and i want to also figure out how to do a live chat. some day!

note note: sometimes you can't see other players, so just try refreshing and eventually it works. lol. wish i could tell you why this happens T_T





✧ what do you need? ✧

unity (i'm currently using ver. 2019.4.2f1)
photon PUN 2 (free ver.)

this is the easy part. you need to make an account with photon. it's free, which lets you have up to 20 users in the server at a time (across all your rooms). create a new application >> the photon type should be "Photon PUN" >> fill the rest out as needed. upon completion, you will be given an app ID. this is important. also, don't share this freely (keep this private!).

after you've done that, open up your unity project and go to the asset store and download + install the free version of photon PUN 2 (not classic). i like to uncheck the demos, since they just take up space and don't need 'em. upon install, the photon wizard should appear and prompt you to enter your app ID. tada! to access the photon network settings, go to window > photon unity networking > highlight server settings (or ctrl+shift+alt+p). for reference my settings are HERE. you will notice i have my RPCs cleared and i also reset my region, too. to be honest i'm not sure how necessary it is but it's what i did. the default settings are usually fine.






✧ photon manager ✧

(before we get started, i have a camera in my scene (referenced as SceneCamera in the script below) which will let you see the scene while it takes a few moments to connect you to the room. in the future i might animate this so it's an ariel view of the world)

first we'll make our "photon manager". this will be a game object that will let us connect to the sever and start/join a room. in your scene, create an empty game object and call it something like, "Photon" (or whatever makes sense for you). create a new script called "MyPhoton" and attach it to our newly created empty. below is the script:

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

public class MyPhoton : MonoBehaviourPunCallbacks
{

//declare your variables
public GameObject playerPrefab;
public GameObject SceneCamera;

// Start is called before the first frame update.
//this will connect to the server using the photon network settings we just mentioned before
void Start()
{
PhotonNetwork.ConnectUsingSettings();
Debug.Log("connected");
}

//this function will connect to a room. if one is full or doesn't exist, a new room will be made.
//you can set the max players per room in roomOptions. i have it set to 20 players
public override void OnConnectedToMaster()
{
RoomOptions roomOptions = new RoomOptions();
roomOptions.MaxPlayers = 20;
PhotonNetwork.JoinOrCreateRoom("Room1", roomOptions, TypedLobby.Default);
Debug.Log("created room");
}

//this function tells unity what to do when it joins a room
//instantiate is what actually spawns your player. vector3 are the coordinates where that player will spawn.
public override void OnJoinedRoom()
{
PhotonNetwork.Instantiate(this.playerPrefab.name, new Vector3(0f, 5f, 0f), Quaternion.identity);
SceneCamera.SetActive(false);
Debug.Log("joined room");
}
}



once you save your script, if you check out "Photon" you will notice two variables that you have to drag your objects in. the SceneCamera, as mentioned before, and the playerPrefab. we will go over this in the next step, but this will be your player. just remember to go back to the "Photon" game object once your prefab is made!

note: the scene camera is not reeaally necessary, so if it annoys you just remove "public GameObject SceneCamera;" and "SceneCamera.SetActive(false);" from the script.






✧ player prefab ✧

to be continued... ;-P