︎︎︎Twitch Chat Game



Tags :
︎ Chat Game

*Tutorial

Role :
Developer



︎ Morphosis 


︎ 2020 06

︎ Project Breakdown


. looking for a fun way to communicate during quarantine 

︎ Shared Code Sample :
Plug Unity into Twitch


. it is possible accessing to twitch via unity in runtime using the sources in the TwitchLib repository 


. focusing on the customization of characters and inputs there are three main edits that can be done to the client manager


01.  randomly load characters from a folder
OnMessageReceived

02. detect user messages 
OnMessageReceived

03.adding the TPC input to the characters 
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
/// <summary>
    /// instantiate gameplayer when someone just join the conversation 
    /// </summary>
    [Tooltip("The player prefab instantiate every time someone joins the chat")]
    public GameObject playerPrefab;
    [Tooltip("The position which the player can be randomly instantiated")]
    public Transform[] InstPositions;

    GameObject checkName;
    public void OnMessageReceived(object sender, TwitchLib.Client.Events.OnMessageReceivedArgs e)
    {

        Debug.Log($"Message received from {e.ChatMessage.Username}: {e.ChatMessage.Message}");

        // here you need build everything up
        // step 1 create avatar based on name 
        // step 2 update - if username text x do y etc.

        // get a random position to instantiate from your public list 
        int randomPlace = Random.Range(0, InstPositions.Length);
        // if there is not the player alredy instantiated with the same name of the chatter joiner 
        if (GameObject.Find(e.ChatMessage.Username) == null)
        {
            // instantiate the avatar 
            GameObject Avatar = Instantiate(playerPrefab, InstPositions[randomPlace].position, InstPositions[randomPlace].rotation);
            // change the name of the game object 
            Avatar.name = e.ChatMessage.Username;
            // change teh name of the label 
            Avatar.transform.GetChild(0).GetComponent<TextMesh>().text = e.ChatMessage.Username;
            // also change randomly the avatar mesh 
            Mesh[] meshArray;
            meshArray = Resources.LoadAll<Mesh>("_MainRig");
            int randomMesh = Random.Range(0, meshArray.Length);
            Avatar.GetComponentInChildren<SkinnedMeshRenderer>().sharedMesh = meshArray[randomMesh];
            // finally send a verification in console 
            Debug.Log("The User " + e.ChatMessage.Username + " is now in the scene!");
        }
        // see on the avatar the TwitchAvatar class to manage the motion of the player. 



        if (GameObject.Find(e.ChatMessage.Username) == true)
        {
            switch (e.ChatMessage.Message)
            {
                case "front":
                    {

                        GameObject player;
                        player = GameObject.Find(e.ChatMessage.Username);
                        StartCoroutine(VerticalInput(player, 0.5f));
                        Debug.Log("this guy shoud be moving front");
                    }
                    break;
                case "right":

                    {
                        GameObject player;
                        player = GameObject.Find(e.ChatMessage.Username);
                        StartCoroutine(HorizontalInput(player, 0.5f));
                        Debug.Log("this guy shoud be moving right");
                    }
                    break;
                case "left":

                    {
                        GameObject player;
                        player = GameObject.Find(e.ChatMessage.Username);
                        StartCoroutine(HorizontalInput(player, -0.5f));
                        Debug.Log("this guy shoud be moving left");
                    }
                    break;
                case "back":

                    {
                        GameObject player;
                        player = GameObject.Find(e.ChatMessage.Username);
                        StartCoroutine(VerticalInput(player, -0.5f));
                        Debug.Log("this guy shoud be moving back");
                    }
                    break;
                case "morph":

                    {
                        Vector3 up = new Vector3(0, 2, 0);
                        GameObject player;
                        player = GameObject.Find(e.ChatMessage.Username);
                        player.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonCharacter>().Move(up, false, true);
                        Debug.Log(" this guy shoud be jumping");
                    }
                    break;
            }
        }
    }     

    IEnumerator HorizontalInput(GameObject player, float direction)
    {
        player.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonUserControl>().h = direction;
        yield return new WaitForSeconds(duration);
        player.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonUserControl>().h = 0;
    }

    IEnumerator VerticalInput(GameObject player, float direction)
    {
        player.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonUserControl>().v = direction;
        yield return new WaitForSeconds(duration);
        player.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonUserControl>().v = 0;
    }