Posted on

Among Us Multiplayer – Instantiate Player Avatar (PUN 2 in Unity)

Today is November 3, 2020, which means it is election day in the United States. We are asking everyone to play our political game Presidential Slap and share it with their friends and family. Presidential Slap allows you to pick which presidential candidate you disagree with the most, whether Trump or Biden and Slap them around a bit.

Play Presidential Slap
Android: https://play.google.com/store/apps/details?id=com.InfoGamer.PresidentialSlap
iOS: https://apps.apple.com/us/app/presidential-slap/id1159421349

For this lesson on how to make Among Us multiplayer with the Photon PUN 2 plugin in Unity, we will show you how to instantiate the player avatar across the network. This will make it so we can control the player locally and synchronize values to the other clients. This is done by first checking to see if the player object is owned by the local player. If this is true, then we will instantiate the player’s avatar across the network.

Unlock Code and Member Content

MyPhotonPlayer.cs

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

public class MyPhotonPlayer : MonoBehaviour
{
    PhotonView myPV;
    GameObject myPlayerAvatar;


    // Start is called before the first frame update
    void Start()
    {
        myPV = GetComponent<PhotonView>();
        if (myPV.IsMine)
        {
            myPlayerAvatar = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "AU_Player"), Vector3.zero, Quaternion.identity);
        }
    }

}
using Photon.Chat;
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PhotonChatManager : MonoBehaviour, IChatClientListener
{
    #region Setup

    [SerializeField] GameObject joinChatButton;
    ChatClient chatClient;
    bool isConnected;
    [SerializeField] string username;

    public void UsernameOnValueChange(string valueIn)
    {
        username = valueIn;
    }

    public void ChatConnectOnClick()
    {
        isConnected = true;
        chatClient = new ChatClient(this);
        //chatClient.ChatRegion = "US";
        chatClient.Connect(PhotonNetwork.PhotonServerSettings.AppSettings.AppIdChat, PhotonNetwork.AppVersion, new AuthenticationValues(username));
        Debug.Log("Connenting");
    }

    #endregion Setup

    #region General

    [SerializeField] GameObject chatPanel;
    string privateReceiver = "";
    string currentChat;
    [SerializeField] InputField chatField;
    [SerializeField] Text chatDisplay;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (isConnected)
        {
            chatClient.Service();
        }

        if (chatField.text != "" &amp;&amp; Input.GetKey(KeyCode.Return))
        {
            SubmitPublicChatOnClick();
            SubmitPrivateChatOnClick();
        }
    }

    #endregion General

    #region PublicChat

    public void SubmitPublicChatOnClick()
    {
        if (privateReceiver == "")
        {
            chatClient.PublishMessage("RegionChannel", currentChat);
            chatField.text = "";
            currentChat = "";
        }
    }

    public void TypeChatOnValueChange(string valueIn)
    {
        currentChat = valueIn;
    }

    #endregion PublicChat

    #region PrivateChat

    public void ReceiverOnValueChange(string valueIn)
    {
        privateReceiver = valueIn;
    }

    public void SubmitPrivateChatOnClick()
    {
        if (privateReceiver != "")
        {
            chatClient.SendPrivateMessage(privateReceiver, currentChat);
            chatField.text = "";
            currentChat = "";
        }
    }

    #endregion PrivateChat

    #region Callbacks

    public void DebugReturn(DebugLevel level, string message)
    {
        //throw new System.NotImplementedException();
    }

    public void OnChatStateChange(ChatState state)
    {
        if(state == ChatState.Uninitialized)
        {
            isConnected = false;
            joinChatButton.SetActive(true);
            chatPanel.SetActive(false);
        }
    }

    public void OnConnected()
    {
        Debug.Log("Connected");
        joinChatButton.SetActive(false);
        chatClient.Subscribe(new string[] { "RegionChannel" });
    }

    public void OnDisconnected()
    {
        isConnected = false;
        joinChatButton.SetActive(true);
        chatPanel.SetActive(false);
    }

    public void OnGetMessages(string channelName, string[] senders, object[] messages)
    {
        string msgs = "";
        for (int i = 0; i &lt; senders.Length; i++)
        {
            msgs = string.Format("{0}: {1}", senders[i], messages[i]);

            chatDisplay.text += "\n" + msgs;

            Debug.Log(msgs);
        }

    }

    public void OnPrivateMessage(string sender, object message, string channelName)
    {
        string msgs = "";

        msgs = string.Format("(Private) {0}: {1}", sender, message);

        chatDisplay.text += "\n " + msgs;

        Debug.Log(msgs);
        
    }

    public void OnStatusUpdate(string user, int status, bool gotMessage, object message)
    {
        throw new System.NotImplementedException();
    }

    public void OnSubscribed(string[] channels, bool[] results)
    {
        chatPanel.SetActive(true);
    }

    public void OnUnsubscribed(string[] channels)
    {
        throw new System.NotImplementedException();
    }

    public void OnUserSubscribed(string channel, string user)
    {
        throw new System.NotImplementedException();
    }

    public void OnUserUnsubscribed(string channel, string user)
    {
        throw new System.NotImplementedException();
    }

    #endregion Callbacks
}
Posted on

Among Us Multiplayer – Photon and Matchmaking (PUN 2 in Unity)

This is the being of our series on how to make Among Us multiplayer. I have decided to implement multiplayer using the Photon PUN 2 plugin instead of Mirror as there were still many of our subscribers who wanted more Photon. In addition, I am still not very familiar with Mirror in order to give you a complete tutorial series on how to use Mirror. However, I would like to create a series on it in the future.

For this lesson, we will import the Photon PUN 2 plugin from the Unity Asset store. After which we will connect our project to our Photon account. Finally, we will import and implement our Info Gamer Quick Start Matchmaking add-on.

If you do not which to purchase this plugin then you can learn to create it here.

We will then set up our project so that our quick start scene loads in to our game scene

Posted on

Among Us in Unity – Level Design (Lesson 9)

In this lesson on how to make Among Us in Unity, we will go over building the environment and level design. This is fairly easy to do as most of the level is already created with the background image that we have added to our project. Now if you are creating your own original game then you will need to create your own background image. Once you have your background or map created and added to your scene you will need to create the walls. These are just 3D cubes with the wall tag and layer and you will need to resize and reposition these objects so they are overlapping all the walls of your map.

Next, you will need to create the objects in your scene that the player can not walk through but do not cast shadows. These are also 3D objects with colliders but are set to a layer that does not cast shadows and that our raycast ignores.